Discord Bot
Host agents as Discord Bots.
The Discord Bot integration allows you to serve Agents or Teams via Discord, using the discord.py library to handle Discord events and send messages.
Setup Steps
Setup and Configuration
Prerequisites
Ensure you have the following:
- Python 3.7+
- A Discord account with server management permissions
- Required Python packages (will be installed in later steps)
Create a Discord Application
- Go to Discord Developer Portal
- Click "New Application"
- Provide an application name (e.g., "My Agno Bot")
- Accept the Developer Terms of Service
- Click "Create"
Create a Bot User
- In your application settings, navigate to the "Bot" section
- Click "Add Bot"
- Confirm by clicking "Yes, do it!"
- Under the "Token" section, click "Copy" to copy your bot token
- Save this token securely (you'll need it later)
Configure Bot Permissions and Intents
- In the Bot settings, scroll down to "Privileged Gateway Intents"
- Enable the following intents:
- Presence Intent (optional, for user status)
- Server Members Intent (for member-related events)
- Message Content Intent (required for reading message content)
- Under "Bot Permissions", ensure your bot has:
- Send Messages
- Read Message History
- Create Public Threads
- Use Slash Commands (optional)
Setup Environment Variables
Create a .envrc
file in your project root with the following content, replacing the placeholder with your actual bot token:
Find your bot token in the Discord Developer Portal under "Bot" > "Token".
Ensure this file is sourced by your shell (e.g., by using direnv allow
).
Install Required Packages
Install the necessary Python packages:
Invite Bot to Your Discord Server
- In your application settings, go to "OAuth2" > "URL Generator"
- Under "Scopes", select:
bot
applications.commands
(if using slash commands)
- Under "Bot Permissions", select the permissions your bot needs:
- Send Messages
- Create Public Threads
- Read Message History
- Attach Files
- Embed Links
- Use External Emojis (optional)
- Copy the generated URL, navigate to it in your browser, and select the server where you want to add the bot
Test Your Bot
- Start your bot application
- Go to your Discord server
- Send a message in any channel where your bot has access
- Your bot should automatically create a thread and respond
- If using a media bot, try uploading an image or file to test media processing
Example Usage
Create an agent, wrap it with DiscordClient
, and run it:
Core Components
DiscordClient
: Wraps Agno agents/teams for Discord integration using discord.py.DiscordClient.serve
: Starts the Discord bot client with the provided token.
DiscordClient
Class
Main entry point for Agno Discord bot applications.
Initialization Parameters
Parameter | Type | Default | Description |
---|---|---|---|
agent | Optional[Agent] | None | Agno Agent instance. |
team | Optional[Team] | None | Agno Team instance. |
Provide agent
or team
, not both.
Event Handling
The Discord bot automatically handles various Discord events:
Message Events
- Description: Processes all incoming messages from users
- Media Support: Handles images, videos, audio files, and documents
- Threading: Automatically creates threads for conversations
- Features:
- Automatic thread creation for each conversation
- Media processing and forwarding to agents
- Message splitting for responses longer than 1500 characters
- Support for reasoning content display
- Context enrichment with username and message URL
Supported Media Types
- Images: Direct URL processing for image analysis
- Videos: Downloads and processes video content
- Audio: URL-based audio processing
- Files: Downloads and processes document attachments
Environment Variables
Ensure the following environment variable is set:
Message Processing
The bot processes messages with the following workflow:
- Message Reception: Receives messages from Discord channels
- Media Processing: Downloads and processes any attached media
- Thread Management: Creates or uses existing threads for conversations
- Agent/Team Execution: Forwards the message and media to the configured agent or team
- Response Handling: Sends the response back to Discord, splitting long messages if necessary
- Reasoning Display: Shows reasoning content in italics if available
Features
Automatic Thread Creation
- Creates a new thread for each user’s first message
- Maintains conversation context within threads
- Uses the format:
{username}'s thread
Media Support
- Images: Passed as
Image
objects with URLs - Videos: Downloaded and passed as
Video
objects with content - Audio: Passed as
Audio
objects with URLs - Files: Downloaded and passed as
File
objects with content
Message Formatting
- Long messages (>1500 characters) are automatically split
- Reasoning content is displayed in italics
- Batch numbering for split messages:
[1/3] message content
Testing the Integration
- Set up your Discord bot token:
export DISCORD_BOT_TOKEN="your-token"
- Run your application:
python your_discord_bot.py
- Invite the bot to your Discord server
- Send a message in any channel where the bot has access
- The bot will automatically create a thread and respond