Skip to content

Commit

Permalink
Added the discord get messages tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maanethdesilva committed Nov 24, 2023
1 parent b2703a8 commit 6bf96cc
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 9 deletions.
1 change: 1 addition & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,7 @@
"@langchain/core": "^0.0.1",
"@langchain/openai": "^0.0.1",
"binary-extensions": "^2.2.0",
"discord.js": "^14.14.1",
"expr-eval": "^2.0.2",
"flat": "^5.0.2",
"js-tiktoken": "^1.0.7",
Expand Down
70 changes: 70 additions & 0 deletions langchain/src/tools/discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { getEnvironmentVariable } from "../util/env.js";
import { Tool } from "./base.js";
import { Client, TextChannel, GatewayIntentBits, Message } from "discord.js";

/*
* A tool for retrieving messages from a discord channel using a bot.
* It extends the base Tool class and implements the _call method to
* perform the retrieve operation. Requires an bot token which can be set
* in the environment variables, and a limit on how many messages to retrieve.
* The _call method takes the discord channel ID as the input argument.
* The bot must have read permissions to the given channel. It returns the
* message content, author, and time the message was created for each message.
*/

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

name = "discord-get-messages";

description = `a discord tool. useful for reading messages from a discord channel.
input should be the discord channel ID. the bot should have read
permissions for the channel`;

protected botToken: string;
protected messageLimit: number;

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

constructor(
botToken: string | undefined = getEnvironmentVariable("DiscordBotToken"),
messageLimit: number | undefined = 100
) {
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;
this.messageLimit = messageLimit;
}

/** @ignore */
async _call(input: string): Promise<string> {
await this.client.login(this.botToken);

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

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

const messages = await channel.messages.fetch({ limit: this.messageLimit });
this.client.destroy();
const results =
messages.map((message: Message) => ({
author: message.author.tag,
content: message.content,
timestamp: message.createdAt,
})) ?? [];

return JSON.stringify(results);
}
}
12 changes: 12 additions & 0 deletions langchain/src/tools/tests/discord.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from "@jest/globals";
import { DiscordGetMessagesTool } from "../discord.js";

test("DiscordGetMessagesTool", async () => {
const tool = new DiscordGetMessagesTool();
try {
const result = await tool.call('1153400523718938780')
console.log(result)
} catch (error) {
console.error(error);
}
});
Loading

0 comments on commit 6bf96cc

Please sign in to comment.