This example demonstrates how to build a Discord analysis bot that categorizes and classifies messages from a help forum to minimize the time needed to understand customer issues.
The Discord Analysis Bot:
- Scrapes messages from the help-and-questions forum in Discord
- Uses an agent to categorize messages based on Mastra concepts (Workflows, Memory, Primitives, Docs, Getting Started)
- Generates a structured analysis with categories, counts, sentiment, and top issues
- Node.js v20.0+
- pnpm (recommended) or npm
- OpenAI API key
- Discord bot token (optional, for real Discord API access)
-
Clone the repository and navigate to the project directory:
git clone https://github.com/mastra-ai/mastra cd examples/discord-analysis-bot -
Install dependencies:
pnpm install
-
Create a
.envfile with your API keys:OPENAI_API_KEY=your_openai_api_key DISCORD_BOT_TOKEN=your_discord_bot_token # Optional DISCORD_CHANNEL_ID=your_channel_id # Optional -
Run the example:
pnpm start
If you want to use real Discord data instead of mock data:
-
Create a Discord application:
- Go to Discord Developer Portal
- Click "New Application" and give it a name
- Navigate to the "Bot" tab and click "Add Bot"
-
Configure bot permissions:
- Under "Privileged Gateway Intents", enable "Message Content Intent"
- This allows the bot to read message content
-
Get your bot token:
- In the Bot tab, click "Reset Token" or "Copy" to get your bot token
- Add this token to your
.envfile asDISCORD_BOT_TOKEN
-
Invite the bot to your server:
- Go to the "OAuth2" tab, then "URL Generator"
- Select "bot" under scopes
- Select permissions: "Read Messages/View Channels", "Read Message History"
- Copy the generated URL and open it in your browser
- Select your server and authorize the bot
-
Get your channel ID:
- Enable Developer Mode in Discord (Settings > Advanced > Developer Mode)
- Right-click on the channel you want to analyze and select "Copy ID"
- Add this ID to your
.envfile asDISCORD_CHANNEL_ID
src/index.ts: Main entry point that runs the Discord analysissrc/mastra/index.ts: Initializes the Mastra instancesrc/mastra/agents/index.ts: Defines the Discord analysis agentsrc/mastra/tools/index.ts: Contains the Discord scraper tool
The example will output:
- Categories of messages (Workflows, Memory, Primitives, etc.)
- Count of messages in each category
- Sentiment analysis for each category
- Top issues identified in each category
- Overall summary of the analysis
This example demonstrates how to build a Discord analysis bot that connects to Discord and fetches actual messages for analysis. The implementation:
- Uses the Discord.js library to connect to Discord and retrieve messages
- Implements proper authentication with a Discord bot token
- Includes error handling and logging for API calls
- Provides structured analysis of message categories and issues
For a production implementation, you might consider adding features like:
- Scheduled analysis runs
- Historical data storage
- Trend analysis over time
- Integration with notification systems