@chat-adapter/discord
Discord adapter for Chat SDK. Configure with HTTP Interactions and Gateway WebSocket support.
Installation
Usage
The adapter auto-detects DISCORD_BOT_TOKEN, DISCORD_PUBLIC_KEY, DISCORD_APPLICATION_ID, and DISCORD_MENTION_ROLE_IDS from environment variables:
Discord application setup
1. Create application
- Go to the Discord Developer Portal
- Click New Application and give it a name
- Note the Application ID from the General Information page
- Copy the Public Key from the General Information page
2. Create bot
- Go to the Bot section in the left sidebar
- Click Reset Token to generate a new bot token
- Copy and save the token (you won't see it again)
- Enable these Privileged Gateway Intents:
- Message Content Intent
- Server Members Intent (if needed)
3. Configure interactions endpoint
- Go to General Information
- Set Interactions Endpoint URL to
https://your-domain.com/api/webhooks/discord - Discord sends a PING to verify the endpoint
4. Add bot to server
- Go to OAuth2 then URL Generator
- Select scopes:
bot,applications.commands - Select bot permissions: Send Messages, Send Messages in Threads, Create Public Threads, Read Message History, Add Reactions, Attach Files
- Copy the generated URL and open it to invite the bot to your server
Architecture: HTTP Interactions vs Gateway
Discord has two ways to receive events:
HTTP Interactions (default):
- Receives button clicks, slash commands, and verification pings
- Works out of the box with serverless
- Does not receive regular messages
Gateway WebSocket (required for messages):
- Receives regular messages and reactions
- Requires a persistent connection
- In serverless environments, use a cron job to maintain the connection
Gateway setup for serverless
1. Create Gateway route
2. Configure Vercel Cron
This runs every 9 minutes, ensuring overlap with the 10-minute listener duration.
3. Add environment variables
Add CRON_SECRET to your Vercel project settings.
Role mentions
By default, only direct user mentions (@BotName) trigger onNewMention handlers. To also trigger on role mentions (e.g., @AI):
- Create a role in your Discord server (e.g., "AI")
- Assign the role to your bot
- Copy the role ID (right-click role in server settings with Developer Mode enabled)
- Add to
mentionRoleIds:
Or set DISCORD_MENTION_ROLE_IDS as a comma-separated string in your environment variables.
Configuration
All options are auto-detected from environment variables when not provided.
*botToken, publicKey, and applicationId are required — either via config or env vars.
Environment variables
Features
Messaging
Rich content
Conversations
Message history
Testing
Run a local tunnel (e.g., ngrok) to test webhooks locally:
Update the Interactions Endpoint URL in the Discord Developer Portal to your ngrok URL.
Troubleshooting
Bot not responding to messages
- Check Gateway connection: Messages require the Gateway WebSocket, not just HTTP interactions
- Verify Message Content Intent: Enable this in the Bot settings
- Check bot permissions: Ensure the bot can read messages in the channel
Role mentions not triggering
- Verify role ID: Enable Developer Mode in Discord settings, then right-click the role
- Check
mentionRoleIdsconfig: Ensure the role ID is in the array - Confirm bot has the role: The bot must have the role assigned
Signature verification failing
- Check public key format: Should be a 64-character hex string (lowercase)
- Verify endpoint URL: Must exactly match what's configured in Discord Developer Portal
- Check for body parsing: Don't parse the request body before verification
License
MIT