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

Mass/discord get messages service #1

Merged
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
1 change: 1 addition & 0 deletions docs/api_refs/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"./langchain/src/tools/aws_lambda.ts",
"./langchain/src/tools/aws_sfn.ts",
"./langchain/src/tools/calculator.ts",
"./langchain/src/tools/discord.ts",
"./langchain/src/tools/render.ts",
"./langchain/src/tools/sql.ts",
"./langchain/src/tools/webbrowser.ts",
Expand Down
3 changes: 3 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ tools/aws_sfn.d.ts
tools/calculator.cjs
tools/calculator.js
tools/calculator.d.ts
tools/discord.cjs
tools/discord.js
tools/discord.d.ts
tools/render.cjs
tools/render.js
tools/render.d.ts
Expand Down
13 changes: 13 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
"tools/calculator.cjs",
"tools/calculator.js",
"tools/calculator.d.ts",
"tools/discord.cjs",
"tools/discord.js",
"tools/discord.d.ts",
"tools/render.cjs",
"tools/render.js",
"tools/render.d.ts",
Expand Down Expand Up @@ -923,6 +926,7 @@
"cohere-ai": ">=6.0.0",
"convex": "^1.3.1",
"d3-dsv": "^2.0.0",
"discord.js": "^14.14.1",
"dotenv": "^16.0.3",
"dpdm": "^3.12.0",
"epub2": "^3.0.1",
Expand Down Expand Up @@ -1041,6 +1045,7 @@
"cohere-ai": ">=6.0.0",
"convex": "^1.3.1",
"d3-dsv": "^2.0.0",
"discord.js": "^14.14.1",
"epub2": "^3.0.1",
"faiss-node": "^0.5.1",
"fast-xml-parser": "^4.2.7",
Expand Down Expand Up @@ -1255,6 +1260,9 @@
"d3-dsv": {
"optional": true
},
"discord.js": {
"optional": true
},
"epub2": {
"optional": true
},
Expand Down Expand Up @@ -1517,6 +1525,11 @@
"import": "./tools/calculator.js",
"require": "./tools/calculator.cjs"
},
"./tools/discord": {
"types": "./tools/discord.d.ts",
"import": "./tools/discord.js",
"require": "./tools/discord.cjs"
},
"./tools/render": {
"types": "./tools/render.d.ts",
"import": "./tools/render.js",
Expand Down
2 changes: 2 additions & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const entrypoints = {
"tools/aws_lambda": "tools/aws_lambda",
"tools/aws_sfn": "tools/aws_sfn",
"tools/calculator": "tools/calculator",
"tools/discord": "tools/discord",
"tools/render": "tools/render",
"tools/sql": "tools/sql",
"tools/webbrowser": "tools/webbrowser",
Expand Down Expand Up @@ -341,6 +342,7 @@ const requiresOptionalDependency = [
"tools/aws_lambda",
"tools/aws_sfn",
"tools/calculator",
"tools/discord",
"tools/sql",
"tools/webbrowser",
"tools/google_calendar",
Expand Down
1 change: 1 addition & 0 deletions langchain/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const optionalImportEntrypoints = [
"langchain/tools/aws_lambda",
"langchain/tools/aws_sfn",
"langchain/tools/calculator",
"langchain/tools/discord",
"langchain/tools/sql",
"langchain/tools/webbrowser",
"langchain/tools/google_calendar",
Expand Down
3 changes: 3 additions & 0 deletions langchain/src/load/import_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export interface OptionalImportMap {
"langchain/tools/calculator"?:
| typeof import("../tools/calculator.js")
| Promise<typeof import("../tools/calculator.js")>;
"langchain/tools/discord"?:
| typeof import("../tools/discord.js")
| Promise<typeof import("../tools/discord.js")>;
"langchain/tools/sql"?:
| typeof import("../tools/sql.js")
| Promise<typeof import("../tools/sql.js")>;
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