Skip to content

Commit

Permalink
Merge pull request #3 from Maanethdesilva/mass/discord-send-messages-…
Browse files Browse the repository at this point in the history
…service

discord send messages service
  • Loading branch information
ArielleRamgoolie authored Nov 27, 2023
2 parents d49128e + 7c3859e commit caba003
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
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)
}
});

0 comments on commit caba003

Please sign in to comment.