Skip to main content

DiscordReader

DiscordReader is a simple data loader that reads all messages in a given Discord channel and returns them as Document objects. It uses the @discordjs/rest library to fetch the messages.

Usage

First step is to create a Discord Application and generating a bot token here. In your Discord Application, go to the OAuth2 tab and generate an invite URL by selecting bot and click Read Messages/View Channels as wells as Read Message History. This will invite the bot with the necessary permissions to read messages. Copy the URL in your browser and select the server you want your bot to join.

import { DiscordReader } from "@llamaindex/readers/discord";

async function main() {
// Create an instance of the DiscordReader. Set token here or DISCORD_TOKEN environment variable
const discordReader = new DiscordReader();

// Specify the channel IDs you want to read messages from as an arry of strings
const channelIds = ["721374320794009630", "719596376261918720"];

// Specify the number of messages to fetch per channel
const limit = 10;

// Load messages from the specified channel
const messages = await discordReader.loadData(channelIds, limit, true);

// Print out the messages
console.log(messages);
}

main().catch(console.error);

Params

DiscordReader()

  • discordToken?: The Discord bot token.
  • requestHandler?: Optionally provide a custom request function for edge environments, e.g. fetch. See discord.js for more info.

DiscordReader.loadData

  • channelIDs: The ID(s) of discord channels as an array of strings.
  • limit?: Optionally limit the number of messages to read
  • additionalInfo?: An optional flag to include embedded messages and attachment urls in the document.
  • oldestFirst?: An optional flag to return the oldest messages first.

API Reference