Skip to content

Commit

Permalink
Merge pull request #6 from Maanethdesilva/mass/discord-update-args
Browse files Browse the repository at this point in the history
updated to single object instead of multiple args
  • Loading branch information
Maanethdesilva authored Dec 6, 2023
2 parents 2059e04 + 652417c commit f1a9a9f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
2 changes: 1 addition & 1 deletion langchain/src/agents/tests/discord.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test.skip("DiscordSendMessagesTool should tell a joke in the discord channel", a
verbose: true,
});

const res = await executor.call({
const res = await executor.invoke({
input: `Tell a joke in the discord channel`,
});

Expand Down
105 changes: 71 additions & 34 deletions langchain/src/tools/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import {
import { getEnvironmentVariable } from "../util/env.js";
import { Tool } from "./base.js";

/**
* Base tool parameters for the Discord tools
*/
interface DiscordToolParams {
botToken?: string;
}

/**
* Tool parameters for the DiscordGetMessagesTool
*/
interface DiscordGetMessagesToolParams extends DiscordToolParams {
messageLimit?: number;
}

/**
* Tool parameters for the DiscordSendMessageTool
*/
interface DiscordSendMessageToolParams extends DiscordToolParams {
channelId?: string;
}

/**
* Tool parameters for the DiscordChannelSearch
*/
interface DiscordChannelSearchParams extends DiscordToolParams {
channelId?: string;
}
/**
* A tool for retrieving messages from a discord channel using a bot.
* It extends the base Tool class and implements the _call method to
Expand Down Expand Up @@ -36,15 +63,17 @@ export class DiscordGetMessagesTool extends Tool {
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

constructor(
botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"),
messageLimit: number | undefined = 10
) {
super(...arguments);
constructor(fields: DiscordGetMessagesToolParams = {}) {
super();

const {
botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"),
messageLimit = 10,
} = fields;

if (!botToken) {
throw new Error(
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file."
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN"
);
}

Expand Down Expand Up @@ -96,14 +125,15 @@ export class DiscordGetGuildsTool extends Tool {
intents: [GatewayIntentBits.Guilds],
});

constructor(
botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN")
) {
super(...arguments);
constructor(fields: DiscordToolParams = {}) {
super();

const { botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN") } =
fields || {};

if (!botToken) {
throw new Error(
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file."
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN"
);
}
this.botToken = botToken;
Expand Down Expand Up @@ -149,14 +179,15 @@ export class DiscordGetTextChannelsTool extends Tool {
intents: [GatewayIntentBits.Guilds],
});

constructor(
botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN")
) {
super(...arguments);
constructor(fields: DiscordToolParams = {}) {
super();

const { botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN") } =
fields || {};

if (!botToken) {
throw new Error(
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file."
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN"
);
}
this.botToken = botToken;
Expand Down Expand Up @@ -186,7 +217,7 @@ export class DiscordGetTextChannelsTool extends Tool {
/**
* A tool for sending messages to a discord channel using a bot.
* It extends the base Tool class and implements the _call method to
* perform the retrieve operation. Requires a bot token which can be set
* perform the retrieve operation. Requires a bot token and channelId which can be set
* in the environment variables. The _call method takes the message to be
* sent as the input argument.
*/
Expand All @@ -208,23 +239,26 @@ export class DiscordSendMessagesTool extends Tool {
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

constructor(
botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"),
channelId: string | undefined = getEnvironmentVariable("DISCORD_CHANNEL_ID")
) {
super(...arguments);
constructor(fields: DiscordSendMessageToolParams = {}) {
super();

const {
botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"),
channelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"),
} = fields;

if (!botToken) {
throw new Error(
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file."
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN"
);
}
this.botToken = botToken;

if (!channelId) {
throw new Error(
"Discord channel not set. You can set it as DISCORD_CHANNEL_ID in your .env file."
"Discord API key not set. You can set it as DISCORD_CHANNEL_ID"
);
}
this.botToken = botToken;
this.channelId = channelId;
}

Expand Down Expand Up @@ -285,23 +319,26 @@ export class DiscordChannelSearchTool extends Tool {
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

constructor(
botToken: string | undefined = getEnvironmentVariable("DISCORD_BOT_TOKEN"),
channelId: string | undefined = getEnvironmentVariable("DISCORD_CHANNEL_ID")
) {
super(...arguments);
constructor(fields: DiscordChannelSearchParams = {}) {
super();

const {
botToken = getEnvironmentVariable("DISCORD_BOT_TOKEN"),
channelId = getEnvironmentVariable("DISCORD_CHANNEL_ID"),
} = fields;

if (!botToken) {
throw new Error(
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN in your .env file."
"Discord API key not set. You can set it as DISCORD_BOT_TOKEN"
);
}
this.botToken = botToken;

if (!channelId) {
throw new Error(
"Discord channel not set. You can set it as DISCORD_CHANNEL_ID in your .env file."
"Discord API key not set. You can set it as DISCORD_CHANNEL_ID"
);
}
this.botToken = botToken;
this.channelId = channelId;
}

Expand Down Expand Up @@ -334,7 +371,7 @@ export class DiscordChannelSearchTool extends Tool {
return JSON.stringify(results);
} catch (err) {
await this.client.destroy();
return "Error sending message";
return "Error searching through channel.";
}
}
}
11 changes: 6 additions & 5 deletions langchain/src/tools/tests/discord.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {

test.skip("DiscordGetMessagesTool", async () => {
const tool = new DiscordGetMessagesTool();

try {
const result = await tool.call("1153400523718938780");
const result = await tool.invoke("1153400523718938780");
console.log(result);
} catch (error) {
console.error(error);
Expand All @@ -20,7 +21,7 @@ test.skip("DiscordGetMessagesTool", async () => {
test.skip("DiscordGetGuildsTool", async () => {
const tool = new DiscordGetGuildsTool();
try {
const result = await tool.call("");
const result = await tool.invoke("");
console.log(result);
} catch (error) {
console.error(error);
Expand All @@ -30,7 +31,7 @@ test.skip("DiscordGetGuildsTool", async () => {
test.skip("DiscordChannelSearchTool", async () => {
const tool = new DiscordChannelSearchTool();
try {
const result = await tool.call("Test");
const result = await tool.invoke("Test");
console.log(result);
} catch (error) {
console.error(error);
Expand All @@ -40,7 +41,7 @@ test.skip("DiscordChannelSearchTool", async () => {
test.skip("DiscordGetTextChannelsTool", async () => {
const tool = new DiscordGetTextChannelsTool();
try {
const result = await tool.call("1153400523718938775");
const result = await tool.invoke("1153400523718938775");
console.log(result);
} catch (error) {
console.error(error);
Expand All @@ -50,7 +51,7 @@ test.skip("DiscordGetTextChannelsTool", async () => {
test.skip("DiscordSendMessagesTool", async () => {
const tool = new DiscordSendMessagesTool();
try {
const result = await tool.call("test message from new code");
const result = await tool.invoke("test message from new code");
console.log(result);
} catch (err) {
console.log(err);
Expand Down

0 comments on commit f1a9a9f

Please sign in to comment.