{"version":3,"file":"discord.cjs","names":["Tool","Client","GatewayIntentBits","ChannelType","TextChannel"],"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,sBAAAA,KAAK;CAC/C,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP,cAAc;;;CAId;CAEA;CAEA;CAEA,YAAY,QAAuC;AACjD,SAAO;EAEP,MAAM,EACJ,YAAA,GAAA,0BAAA,wBAAkC,oBAAoB,EACtD,eAAe,IACf,WACE,UAAU,EAAE;AAEhB,MAAI,CAAC,SACH,OAAM,IAAI,MACR,8FACD;AAGH,OAAK,SACH,UACA,IAAIC,WA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