Installation
Environment Variables
Create a Discord app at the Discord Developer Portal.
| Variable | Required | Description |
|---|---|---|
DISCORD_BOT_TOKEN | Yes | Bot token from Discord Developer Portal |
DISCORD_PUBLIC_KEY | Yes | Public key for webhook signature verification |
DISCORD_APPLICATION_ID | Yes | Application ID (also the bot’s user ID) |
DISCORD_MENTION_ROLE_IDS | Optional | Comma-separated role IDs to treat as mentions |
Configuration Options
Setup
Webhook Handler (HTTP Interactions)
Discord HTTP Interactions are recommended for serverless environments. Set your Interactions Endpoint URL in the Discord Developer Portal.
Gateway Mode (WebSocket)
For receiving all messages (not just interactions):Gateway Event Forwarding
Forward Gateway events to your webhook endpoint:Features
Supported Events
HTTP Interactions:PING- Discord verification (auto-handled)APPLICATION_COMMAND- Slash commandsMESSAGE_COMPONENT- Button clicks
startGatewayListener):
MESSAGE_CREATE- All messages in channels/DMsMESSAGE_REACTION_ADD- Reaction addedMESSAGE_REACTION_REMOVE- Reaction removed
Slash Commands
Register slash commands via Discord Developer Portal, then handle them:Buttons & Components
Create messages with buttons:Embeds
Cards are converted to Discord embeds:Reactions
Add/remove emoji reactions:File Uploads
Send files with messages:Discord supports up to 10 files per message, max 25 MB total (50 MB with Nitro).
Threads
Discord threads are automatically detected:Thread IDs
Discord thread IDs encode guild, channel, and thread:- DM:
discord:@me:987654321 - Channel:
discord:123456789:987654321 - Thread:
discord:123456789:987654321:111222333
Opening DMs
Create a DM channel with a user:Message History
Fetch message history from a channel or thread:Platform Limits
- Message length: 2,000 characters
- Embed description: 4,096 characters
- Embeds per message: 10 embeds
- Files per message: 10 files (25 MB total, 50 MB with Nitro)
- Rate limits: Varies by endpoint, typically 5 requests/5 seconds per route
Code Examples
Troubleshooting
Webhook verification fails (401)
Webhook verification fails (401)
- Verify
DISCORD_PUBLIC_KEYmatches the Public Key in Developer Portal - Ensure public key is lowercase hex (64 characters)
- Check that signature headers are present:
x-signature-ed25519,x-signature-timestamp
Bot doesn't respond to messages
Bot doesn't respond to messages
- HTTP Interactions only work for slash commands and buttons
- Use Gateway mode (
startGatewayListener) to receive all messages - Ensure bot has
MESSAGE CONTENTintent enabled in Developer Portal
Slash commands not showing
Slash commands not showing
- Register commands via Discord Developer Portal or REST API
- Commands can take up to 1 hour to sync globally
- Use guild-specific commands for instant updates during development
Gateway listener disconnects
Gateway listener disconnects
- Gateway connections require persistent runtime (not serverless)
- Use AbortSignal to gracefully stop when deploying new version
- Forward Gateway events to webhook for serverless processing
Required Intents & Scopes
Bot Permissions:VIEW_CHANNEL- See channelsSEND_MESSAGES- Send messagesEMBED_LINKS- Send embedsATTACH_FILES- Upload filesADD_REACTIONS- Add reactionsREAD_MESSAGE_HISTORY- Fetch message historyUSE_SLASH_COMMANDS- Respond to slash commands
GUILDS- Basic guild infoGUILD_MESSAGES- Receive messagesMESSAGE_CONTENT- Read message content (privileged)DIRECT_MESSAGES- Receive DMsGUILD_MESSAGE_REACTIONS- Receive reactionsDIRECT_MESSAGE_REACTIONS- Receive DM reactions
Next Steps
Message Handling
Process messages and build conversation flows
Embeds & Components
Create rich embeds with buttons
Discord Developer Docs
Learn about Discord’s Bot API
State Management
Persist data across requests