Skip to content
Sign up

Create a Discord bot

Build Discord bots powered by Letta agents with persistent memory and intelligent conversation capabilities.

Create a Discord bot powered by a Letta agent with persistent memory, streaming responses, and thread support.

Terminal window
git clone https://github.com/letta-ai/letta-discord-bot-example.git
cd letta-discord-bot-example
npm install
  1. Create an application in the Discord Developer Portal
  2. Under Bot, copy your token and enable Message Content Intent
  3. Under Installation, set scopes to bot, applications.commands with permissions: Read Messages, Send Messages, Create Public Threads, Send Messages in Threads, Read Message History
  4. Add the bot to your server using the installation link
Terminal window
cp .env.template .env

Edit .env:

Terminal window
# Letta
LETTA_API_KEY=your_letta_api_key
LETTA_BASE_URL=https://api.letta.com # or http://localhost:8283 for self-hosted
LETTA_AGENT_ID=your_agent_id
# Discord
APP_ID=your_discord_app_id
DISCORD_TOKEN=your_discord_bot_token
PUBLIC_KEY=your_discord_public_key
# Behavior
RESPOND_TO_DMS=true
RESPOND_TO_MENTIONS=true
RESPOND_TO_BOTS=false
RESPOND_TO_GENERIC=false
Terminal window
npm run build && npm start
  1. Discord message received and filtered by type
  2. Recent conversation history fetched for context
  3. Message formatted with sender info and channel name
  4. Streamed to your Letta agent
  5. Response streamed back to Discord (auto-split if over 2000 chars)
TypeTriggerFormat
DMDirect message[user (id=123) sent you a DM] message
MENTION@bot[user (id=123) mentioned you in #channel] message
REPLYReply to bot[user (id=123) replied to you in #channel] message
GENERICAny message[user (id=123) in #channel] message
Terminal window
LETTA_CONTEXT_MESSAGE_COUNT=5 # Recent messages to include (0 to disable)
LETTA_THREAD_CONTEXT_ENABLED=true # Fetch full thread context
LETTA_THREAD_MESSAGE_LIMIT=50 # Max thread messages
Terminal window
DISCORD_CHANNEL_ID=123 # Only listen to this channel
DISCORD_RESPONSE_CHANNEL_ID=456 # Only respond in this channel
Terminal window
REPLY_IN_THREADS=true # Create threads for responses
MESSAGE_BATCH_ENABLED=true # Batch messages before sending
MESSAGE_BATCH_SIZE=10 # Max messages per batch
MESSAGE_BATCH_TIMEOUT_MS=30000 # Auto-send after 30s
Terminal window
ENABLE_TIMER=true # Allow agent-initiated messages
TIMER_INTERVAL_MINUTES=15 # Max interval
FIRING_PROBABILITY=0.1 # 10% chance per interval

Requires DISCORD_CHANNEL_ID to be set.

Add Discord context to your agent’s human block:

I am connected to a Discord server.
I see messages when users mention me or reply to me.
To mention a user, use <@discord-id> format.

Add an ignore() tool to let your agent skip messages:

def ignore():
"""Call this to skip responding to a message."""
return

Deploy on Railway

See the self-hosting guide for running your own Letta server. Once running, set LETTA_BASE_URL=http://localhost:8283 in your .env.

Run the bot with PM2, systemd, or Docker to keep it running:

Terminal window
npm run build && npm start

Bot doesn’t respond: Check Message Content Intent is enabled, RESPOND_TO_MENTIONS=true, and bot has channel permissions.

Agent doesn’t remember context: Verify LETTA_CONTEXT_MESSAGE_COUNT > 0 and thread context is enabled if using threads.

Errors in Discord: Set SURFACE_ERRORS=true to see errors in chat (default: logged only).