Skip to main content

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

The DiscordService class is the main entry point for Discord functionality:

Key Responsibilities:

  1. Client Initialization
    • Creates Discord.js client with required intents
    • Handles authentication with bot token
    • Manages connection lifecycle
  2. Event Registration
    • Listens for Discord events (messages, interactions, etc.)
    • Routes events to appropriate handlers
    • Manages event cleanup on disconnect
  3. Channel Restrictions
    • Parses CHANNEL_IDS environment variable
    • Enforces channel-based access control
    • Filters messages based on allowed channels
  4. Component Coordination
    • Initializes MessageManager and VoiceManager
    • Coordinates between different components
    • Manages shared state and resources

Message Manager

The MessageManager class handles all message-related operations:

Message Processing Flow:

  1. Message Reception
  2. Format Conversion
  3. Attachment Processing
  4. Response Handling

Voice Manager

The VoiceManager class manages voice channel operations:

Voice Features:

  1. Connection Management
    • Join/leave voice channels
    • Handle connection state changes
    • Manage multiple connections
  2. Audio Processing
    • Capture audio streams
    • Process voice activity
    • Handle speaker changes
  3. 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

  1. Token Security
  2. Error Recovery
  3. Resource Cleanup
  4. Monitoring

Debugging

Enable debug logging:
Common debug points:
  • Connection establishment
  • Message processing pipeline
  • Voice connection state
  • Permission checks
  • API rate limits

Support

For issues and questions: