Overview
The@elizaos/plugin-discord
package provides comprehensive Discord integration for elizaOS agents. It enables agents to operate as fully-featured Discord bots with support for text channels, voice channels, direct messages, slash commands, and media processing.
This plugin handles all Discord-specific functionality including:
- Initializing and managing the Discord bot connection
- Processing messages and interactions across multiple servers
- Managing voice channel connections and audio processing
- Handling media attachments and transcription
- Implementing Discord-specific actions and state providers
- Supporting channel restrictions and permission management
Architecture Overview
Core Components
Discord Service
TheDiscordService
class is the main entry point for Discord functionality:
Key Responsibilities:
-
Client Initialization
- Creates Discord.js client with required intents
- Handles authentication with bot token
- Manages connection lifecycle
-
Event Registration
- Listens for Discord events (messages, interactions, etc.)
- Routes events to appropriate handlers
- Manages event cleanup on disconnect
-
Channel Restrictions
- Parses
CHANNEL_IDS
environment variable - Enforces channel-based access control
- Filters messages based on allowed channels
- Parses
-
Component Coordination
- Initializes MessageManager and VoiceManager
- Coordinates between different components
- Manages shared state and resources
Message Manager
TheMessageManager
class handles all message-related operations:
Message Processing Flow:
-
Message Reception
-
Format Conversion
-
Attachment Processing
-
Response Handling
Voice Manager
TheVoiceManager
class manages voice channel operations:
Voice Features:
-
Connection Management
- Join/leave voice channels
- Handle connection state changes
- Manage multiple connections
-
Audio Processing
- Capture audio streams
- Process voice activity
- Handle speaker changes
-
Transcription Integration
- Send audio to transcription services
- Process transcribed text
- Generate responses
Attachment Handler
Processes various types of Discord attachments:Event Processing Flow
1. Guild Join Event
2. Message Create Event
3. Interaction Create Event
Actions
chatWithAttachments
Handles messages that include media attachments:joinVoice
Connects the bot to a voice channel:transcribeMedia
Transcribes audio or video files:Providers
channelStateProvider
Provides current Discord channel context:voiceStateProvider
Provides voice channel state information:Configuration
Environment Variables
Bot Permissions
Required Discord permissions:Bot Invitation
Generate an invitation URL:Multi-Server Architecture
The plugin supports operating across multiple Discord servers simultaneously:Server Isolation
Each server maintains its own:- Conversation context
- User relationships
- Channel states
- Voice connections
Command Registration
Slash commands are registered per-server:Permission Management
Permission Checking
Before performing actions:Error Handling
Handle permission errors gracefully:Performance Optimization
Message Caching
Cache frequently accessed data:Rate Limiting
Implement rate limiting for API calls:Voice Connection Pooling
Reuse voice connections:Error Handling
Connection Errors
Handle Discord connection issues:API Errors
Handle Discord API errors:Integration Guide
Basic Setup
Custom Actions
Add Discord-specific actions:Event Handlers
Listen for Discord-specific events:Best Practices
-
Token Security
-
Error Recovery
-
Resource Cleanup
-
Monitoring
Debugging
Enable debug logging:- Connection establishment
- Message processing pipeline
- Voice connection state
- Permission checks
- API rate limits
Support
For issues and questions:- 📚 Check the examples
- 💬 Join our Discord community
- 🐛 Report issues on GitHub