forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the discord get messages tool.
- Loading branch information
1 parent
b2703a8
commit 6bf96cc
Showing
4 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
Oops, something went wrong.