From 7c3859e5d8f301f87e6a17dc24a48311e9dcd789 Mon Sep 17 00:00:00 2001 From: your_username Date: Mon, 27 Nov 2023 17:05:04 -0500 Subject: [PATCH] Added send messages tool --- langchain/src/tools/discord.ts | 73 +++++++++++++++++++++++ langchain/src/tools/tests/discord.test.ts | 12 +++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/langchain/src/tools/discord.ts b/langchain/src/tools/discord.ts index 31336b6bc3b1..f104a2b6d89a 100644 --- a/langchain/src/tools/discord.ts +++ b/langchain/src/tools/discord.ts @@ -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 { + + 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"; + } + } +} \ No newline at end of file diff --git a/langchain/src/tools/tests/discord.test.ts b/langchain/src/tools/tests/discord.test.ts index 981c94c86c41..3b3d4d19115c 100644 --- a/langchain/src/tools/tests/discord.test.ts +++ b/langchain/src/tools/tests/discord.test.ts @@ -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(); @@ -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) + } }); \ No newline at end of file