Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discord send messages service #3

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions langchain/src/tools/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,76 @@ export class DiscordGetMessagesTool extends Tool {
return JSON.stringify(results);
}
}

/*
* 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
* in the environment variables. The _call method takes the message to be
* sent as the input argument.
*/

export class DiscordSendMessagesTool extends Tool {
static lc_name() {
return "DiscordSendMessagesTool";
}

name = "discord-send-messages";

description = `A discord tool useful for sending messages to a discod channel.
Input should be the discord channel message, since we will already have the channel ID.`;

protected botToken: string;
protected channelId: string;

client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

constructor(
channelId: string,
botToken: string | undefined = getEnvironmentVariable("DiscordBotToken")
) {
super(...arguments);

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

/** @ignore */
async _call(message: string): Promise<string> {

await this.client.login(this.botToken);

const channel = (await this.client.channels.fetch(this.channelId)) as TextChannel;

if (!channel) {
return "Channel not found";
}

if (!(channel instanceof TextChannel)) {
return "Channel is not text channel, cannot send message"
}

try {
await channel.send(message);
this.client.destroy();
return "Message sent successfully";
}
catch (err) {
this.client.destroy();
console.log("Error sending message:", err);
return "Error sending message";
}
}
}
12 changes: 11 additions & 1 deletion langchain/src/tools/tests/discord.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "@jest/globals";
import { DiscordGetMessagesTool } from "../discord.js";
import { DiscordGetMessagesTool, DiscordSendMessagesTool } from "../discord.js";

test("DiscordGetMessagesTool", async () => {
const tool = new DiscordGetMessagesTool();
Expand All @@ -9,4 +9,14 @@ test("DiscordGetMessagesTool", async () => {
} catch (error) {
console.error(error);
}
});

test("DiscordSendMessagesTool", async () => {
const tool = new DiscordSendMessagesTool("1153400523718938780");
try{
const result = await tool.call("test message from new code");
console.log(result)
} catch(err){
console.log(err)
}
});