{"version":3,"file":"discord.cjs","names":["Tool","fields?: DiscordGetMessagesToolParams","Client","GatewayIntentBits","input: string","message: Message","fields?: DiscordToolParams","_input: string","ChannelType","fields?: DiscordSendMessageToolParams","message: string","TextChannel","fields?: DiscordChannelSearchParams","searchTerm: string"],"sources":["../../src/tools/discord.ts"],"sourcesContent":["import {\n  Client,\n  TextChannel,\n  GatewayIntentBits,\n  Message,\n  ChannelType,\n} from \"discord.js\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { Tool } from \"@langchain/core/tools\";\n\n/**\n * Base tool parameters for the Discord tools\n */\ninterface DiscordToolParams {\n  botToken?: string;\n  client?: Client;\n}\n\n/**\n * Tool parameters for the DiscordGetMessagesTool\n */\ninterface DiscordGetMessagesToolParams extends DiscordToolParams {\n  messageLimit?: number;\n}\n\n/**\n * Tool parameters for the DiscordSendMessageTool\n */\ninterface DiscordSendMessageToolParams extends DiscordToolParams {\n  channelId?: string;\n}\n\n/**\n * Tool parameters for the DiscordChannelSearch\n */\ninterface DiscordChannelSearchParams extends DiscordToolParams {\n  channelId?: string;\n}\n/**\n * A tool for retrieving messages from a discord channel using a bot.\n * It extends the base Tool class and implements the _call method to\n * perform the retrieve operation. Requires an bot token which can be set\n * in the environment variables, and a limit on how many messages to retrieve.\n * The _call method takes the discord channel ID as the input argument.\n * The bot must have read permissions to the given channel. It returns the\n * message content, author, and time the message was created for each message.\n */\nexport class DiscordGetMessagesTool extends Tool {\n  static lc_name() {\n    return \"DiscordGetMessagesTool\";\n  }\n\n  name = \"discord-get-messages\";\n\n  description = `A discord tool. useful for reading messages from a discord channel. \n  Input should be the discord channel ID. The bot should have read \n  permissions for the channel.`;\n\n  protected botToken: string;\n\n  protected messageLimit: number;\n\n  protected client: Client;\n\n  constructor(fields?: DiscordGetMessagesToolParams) {\n    super();\n\n    const {\n      botToken = getEnvironmentVariable(\"DISCORD_BOT_TOKEN\"),\n      messageLimit = 10,\n      client,\n    } = fields ?? {};\n\n    if (!botToken) {\n      throw new Error(\n        \"Environment variable DISCORD_BOT_TOKEN missing, but is required for DiscordGetMessagesTool.\"\n      );\n    }\n\n    this.client =\n      client ??\n      new Client({\n        intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],\n      });\n\n    this.botToken = botToken;\n    this.messageLimit = messageLimit;\n  }\n\n  /** @ignore */\n  async _call(input: string): Promise<string> {\n    try {\n      await this.client.login(this.botToken);\n\n      const channel = (await this.client.channels.fetch(input)) as TextChannel;\n\n      if (!channel) {\n        return \"Channel not found.\";\n      }\n\n      const messages = await channel.messages.fetch({\n        limit: this.messageLimit,\n      });\n      await this.client.destroy();\n      const results =\n        messages.map((message: Message) => ({\n          author: message.author.tag,\n          content: message.content,\n          timestamp: message.createdAt,\n        })) ?? [];\n\n      return JSON.stringify(results);\n    } catch {\n      await this.client.destroy();\n      return \"Error getting messages.\";\n    }\n  }\n}\n\n/**\n * A tool for retrieving all servers a bot is a member of. It extends the\n * base `Tool` class and implements the `_call` method to perform the retrieve\n * operation. Requires a bot token which can be set in the environment\n * variables.\n */\nexport class DiscordGetGuildsTool extends Tool {\n  static lc_name() {\n    return \"DiscordGetGuildsTool\";\n  }\n\n  name = \"discord-get-guilds\";\n\n  description = `A discord tool. Useful for getting a list of all servers/guilds the bot is a member of. No input required.`;\n\n  protected botToken: string;\n\n  protected client: Client;\n\n  constructor(fields?: DiscordToolParams) {\n    super();\n\n    const { botToken = getEnvironmentVariable(\"DISCORD_BOT_TOKEN\"), client } =\n      fields ?? {};\n\n    if (!botToken) {\n      throw new Error(\n        \"Environment variable DISCORD_BOT_TOKEN missing, but is required for DiscordGetGuildsTool.\"\n      );\n    }\n    this.client =\n      client ??\n      new Client({\n        intents: [GatewayIntentBits.Guilds],\n      });\n\n    this.botToken = botToken;\n  }\n\n  /** @ignore */\n  async _call(_input: string): Promise<string> {\n    try {\n      await this.client.login(this.botToken);\n\n      const guilds = await this.client.guilds.fetch();\n      await this.client.destroy();\n\n      const results =\n        guilds.map((guild) => ({\n          id: guild.id,\n          name: guild.name,\n          createdAt: guild.createdAt,\n        })) ?? [];\n\n      return JSON.stringify(results);\n    } catch {\n      await this.client.destroy();\n      return \"Error getting guilds.\";\n    }\n  }\n}\n\n/**\n * A tool for retrieving text channels within a server/guild a bot is a member\n * of. It extends the base `Tool` class and implements the `_call` method to\n * perform the retrieve operation. Requires a bot token which can be set in\n * the environment variables. The `_call` method takes a server/guild ID\n * to get its text channels.\n */\nexport class DiscordGetTextChannelsTool extends Tool {\n  static lc_name() {\n    return \"DiscordGetTextChannelsTool\";\n  }\n\n  name = \"discord-get-text-channels\";\n\n  description = `A discord tool. Useful for getting a list of all text channels in a server/guild. Input should be a discord server/guild ID.`;\n\n  protected botToken: string;\n\n  protected client: Client;\n\n  constructor(fields?: DiscordToolParams) {\n    super();\n\n    const { botToken = getEnvironmentVariable(\"DISCORD_BOT_TOKEN\"), client } =\n      fields ?? {};\n\n    if (!botToken) {\n      throw new Error(\n        \"Environment variable DISCORD_BOT_TOKEN missing, but is required for DiscordGetTextChannelsTool.\"\n      );\n    }\n    this.client =\n      client ??\n      new Client({\n        intents: [GatewayIntentBits.Guilds],\n      });\n    this.botToken = botToken;\n  }\n\n  /** @ignore */\n  async _call(input: string): Promise<string> {\n    try {\n      await this.client.login(this.botToken);\n\n      const guild = await this.client.guilds.fetch(input);\n      const channels = await guild.channels.fetch();\n      await this.client.destroy();\n\n      const results =\n        channels\n          .filter((channel) => channel?.type === ChannelType.GuildText)\n          .map((channel) => ({\n            id: channel?.id,\n            name: channel?.name,\n            createdAt: channel?.createdAt,\n          })) ?? [];\n\n      return JSON.stringify(results);\n    } catch {\n      await this.client.destroy();\n      return \"Error getting text channels.\";\n    }\n  }\n}\n\n/**\n * A tool for sending messages to a discord channel using a bot.\n * It extends the base Tool class and implements the _call method to\n * perform the retrieve operation. Requires a bot token and channelId which can be set\n * in the environment variables. The _call method takes the message to be\n * sent as the input argument.\n */\nexport class DiscordSendMessagesTool extends Tool {\n  static lc_name() {\n    return \"DiscordSendMessagesTool\";\n  }\n\n  name = \"discord-send-messages\";\n\n  description = `A discord tool useful for sending messages to a discod channel.\n  Input should be the discord channel message, since we will already have the channel ID.`;\n\n  protected botToken: string;\n\n  protected channelId: string;\n\n  protected client: Client;\n\n  constructor(fields?: DiscordSendMessageToolParams) {\n    super();\n\n    const {\n      botToken = getEnvironmentVariable(\"DISCORD_BOT_TOKEN\"),\n      channelId = getEnvironmentVariable(\"DISCORD_CHANNEL_ID\"),\n      client,\n    } = fields ?? {};\n\n    if (!botToken) {\n      throw new Error(\n        \"Environment variable DISCORD_BOT_TOKEN missing, but is required for DiscordSendMessagesTool.\"\n      );\n    }\n\n    if (!channelId) {\n      throw new Error(\n        \"Environment variable DISCORD_CHANNEL_ID missing, but is required for DiscordSendMessagesTool.\"\n      );\n    }\n\n    this.client =\n      client ??\n      new Client({\n        intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],\n      });\n\n    this.botToken = botToken;\n    this.channelId = channelId;\n  }\n\n  /** @ignore */\n  async _call(message: string): Promise<string> {\n    try {\n      await this.client.login(this.botToken);\n\n      const channel = (await this.client.channels.fetch(\n        this.channelId\n      )) as TextChannel;\n\n      if (!channel) {\n        throw new Error(\"Channel not found\");\n      }\n\n      if (!(channel.constructor === TextChannel)) {\n        throw new Error(\"Channel is not text channel, cannot send message\");\n      }\n\n      await channel.send(message);\n\n      await this.client.destroy();\n\n      return \"Message sent successfully.\";\n    } catch {\n      await this.client.destroy();\n      return \"Error sending message.\";\n    }\n  }\n}\n\n/**\n * A tool for searching for messages within a discord channel using a bot.\n * It extends the base Tool class and implements the _call method to\n * perform the retrieve operation. Requires an bot token which can be set\n * in the environment variables, and the discord channel ID of the channel.\n * The _call method takes the search term as the input argument.\n * The bot must have read permissions to the given channel. It returns the\n * message content, author, and time the message was created for each message.\n */\nexport class DiscordChannelSearchTool extends Tool {\n  static lc_name() {\n    return \"DiscordChannelSearchTool\";\n  }\n\n  name = \"discord_channel_search_tool\";\n\n  description = `A discord toolkit. Useful for searching for messages \n  within a discord channel. Input should be the search term. The bot \n  should have read permissions for the channel.`;\n\n  protected botToken: string;\n\n  protected channelId: string;\n\n  protected client: Client;\n\n  constructor(fields?: DiscordChannelSearchParams) {\n    super();\n\n    const {\n      botToken = getEnvironmentVariable(\"DISCORD_BOT_TOKEN\"),\n      channelId = getEnvironmentVariable(\"DISCORD_CHANNEL_ID\"),\n      client,\n    } = fields ?? {};\n\n    if (!botToken) {\n      throw new Error(\n        \"Environment variable DISCORD_BOT_TOKEN missing, but is required for DiscordChannelSearchTool.\"\n      );\n    }\n\n    if (!channelId) {\n      throw new Error(\n        \"Environment variable DISCORD_CHANNEL_ID missing, but is required for DiscordChannelSearchTool.\"\n      );\n    }\n\n    this.client =\n      client ??\n      new Client({\n        intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],\n      });\n\n    this.botToken = botToken;\n    this.channelId = channelId;\n  }\n\n  /** @ignore */\n  async _call(searchTerm: string): Promise<string> {\n    try {\n      await this.client.login(this.botToken);\n\n      const channel = (await this.client.channels.fetch(\n        this.channelId\n      )) as TextChannel;\n\n      if (!channel) {\n        return \"Channel not found\";\n      }\n\n      const messages = await channel.messages.fetch();\n      await this.client.destroy();\n      const filtered = messages.filter((message) =>\n        message.content.toLowerCase().includes(searchTerm.toLowerCase())\n      );\n\n      const results =\n        filtered.map((message) => ({\n          author: message.author.tag,\n          content: message.content,\n          timestamp: message.createdAt,\n        })) ?? [];\n\n      return JSON.stringify(results);\n    } catch {\n      await this.client.destroy();\n      return \"Error searching through channel.\";\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA+CA,IAAa,yBAAb,cAA4CA,4BAAK;CAC/C,OAAO,UAAU;AACf,SAAO;CACR;CAED,OAAO;CAEP,cAAc,CAAC;;8BAEa,CAAC;CAE7B,AAAU;CAEV,AAAU;CAEV,AAAU;CAEV,YAAYC,QAAuC;EACjD,OAAO;EAEP,MAAM,EACJ,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