Back to Eliza

Discord Agent Examples

packages/examples/discord/README.md

2.0.14.2 KB
Original Source

Discord Agent Examples

Full-featured Discord AI agents using elizaOS, available in TypeScript, Python, and Rust.

Features

  • šŸ¤– Responds to @mentions and replies
  • ⚔ Slash commands (/ping, /about, /help)
  • šŸ’¾ Persistent memory via SQL database
  • 🧠 OpenAI-powered language understanding
  • šŸŽÆ Configurable response behavior

Quick Start

1. Install Dependencies (from repo root)

bash
# Install all dependencies
bun install
bun run build

2. Set Up Discord Bot

  1. Go to Discord Developer Portal
  2. Create a new application
  3. Go to "Bot" section and create a bot
  4. Enable "Message Content Intent" (required for reading messages)
  5. Copy the Bot Token
  6. Invite the bot to your server using OAuth2 URL Generator:
    • Select "bot" scope
    • Select permissions: Send Messages, Read Message History, Add Reactions

3. Configure Environment

bash
cd examples/discord
cp env.example .env
# Edit .env with your credentials

Required variables:

  • DISCORD_APPLICATION_ID - Your Discord application ID
  • DISCORD_API_TOKEN - Your bot token
  • OPENAI_API_KEY - Your OpenAI API key

4. Run the Agent

Choose your preferred language:

TypeScript

bash
cd typescript
bun install
bun start
# or for development with hot reload:
bun dev

Python

bash
cd python
pip install -r requirements.txt
python agent.py

Rust

bash
cd rust/discord-agent
cargo run --release

Project Structure

examples/discord/
ā”œā”€ā”€ env.example              # Environment template
ā”œā”€ā”€ README.md               # This file
ā”œā”€ā”€ typescript/             # TypeScript implementation
│   ā”œā”€ā”€ agent.ts           # Main entry point
│   ā”œā”€ā”€ character.ts       # Bot personality
│   ā”œā”€ā”€ handlers.ts        # Event handlers
│   ā”œā”€ā”€ package.json
│   └── __tests__/         # Tests
ā”œā”€ā”€ python/                 # Python implementation
│   ā”œā”€ā”€ agent.py           # Main entry point
│   ā”œā”€ā”€ character.py       # Bot personality
│   ā”œā”€ā”€ handlers.py        # Event handlers
│   ā”œā”€ā”€ requirements.txt
│   └── tests/             # Tests
└── rust/                   # Rust implementation
    └── discord-agent/
        ā”œā”€ā”€ Cargo.toml
        ā”œā”€ā”€ src/
        │   ā”œā”€ā”€ main.rs    # Main entry point
        │   ā”œā”€ā”€ character.rs # Bot personality
        │   └── handlers.rs  # Event handlers
        └── tests/         # Tests

Customization

Modify Bot Personality

Edit the character file for your language:

  • TypeScript: typescript/character.ts
  • Python: python/character.py
  • Rust: rust/discord-agent/src/character.rs

Add Custom Commands

Edit the handlers file to add new slash commands:

  • TypeScript: typescript/handlers.ts
  • Python: python/handlers.py
  • Rust: rust/discord-agent/src/handlers.rs

Discord Settings

Configure bot behavior in the character settings:

json
{
  "discord": {
    "shouldIgnoreBotMessages": true,
    "shouldRespondOnlyToMentions": true
  }
}

Commands

CommandDescription
/pingCheck if the bot is online
/aboutLearn about the bot
/helpShow available commands

Testing

bash
# TypeScript
cd typescript && bun test

# Python
cd python && pytest

# Rust
cd rust/discord-agent && cargo test

Troubleshooting

Bot not responding to messages

  • Ensure "Message Content Intent" is enabled in Discord Developer Portal
  • Check that the bot has proper permissions in your server
  • Verify DISCORD_API_TOKEN is correct

Slash commands not appearing

  • Commands may take up to an hour to propagate globally
  • For instant testing, use guild-specific commands in development

Rate limiting

  • Discord has rate limits; the bot handles these automatically
  • If you see 429 errors, reduce message frequency

Multi-Platform Setup

This example can work alongside the Telegram example. Both share the same .env file and can run simultaneously for a multi-platform bot experience.

bash
# Run Discord bot
cd examples/discord && bun start &

# Run Telegram bot (in another terminal)
cd examples/telegram && bun start &

License

MIT