Discord Integration
Discord Integration
Section titled “Discord Integration”OpenClaw integrates with Discord via the official Discord Bot API. This allows you to create powerful Discord bots that can handle direct messages, server channels, slash commands, and rich interactions.
Quick Setup
Section titled “Quick Setup”Prerequisites
Section titled “Prerequisites”- Discord account
- Discord server (create one for testing)
- Bot token from Discord Developer Portal
Basic Configuration
Section titled “Basic Configuration”-
Create a Discord bot:
- Go to Discord Developer Portal
- Create new application → Bot
- Copy the bot token
-
Enable required intents:
- Message Content Intent
- Server Members Intent (for allowlists)
- Direct Messages Intent
-
Configure OpenClaw (
~/.openclaw/openclaw.json):
{ channels: { discord: { enabled: true, token: "YOUR_BOT_TOKEN_HERE" } }}-
Invite bot to server:
- Generate OAuth2 URL in Developer Portal
- Include bot permissions: Read Messages/View Channels, Send Messages, Embed Links
-
Start the gateway:
openclaw gatewayConfiguration Options
Section titled “Configuration Options”Basic Settings
Section titled “Basic Settings”{ channels: { discord: { // Bot configuration enabled: true, token: "YOUR_BOT_TOKEN_HERE",
// Gateway intents intents: [ "GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES", "MESSAGE_CONTENT" ],
// Direct message policy dm: { policy: "pairing", // "pairing", "allowlist", "denylist", "open" allowFrom: ["123456789012345678"], // User IDs blockFrom: ["987654321098765432"], groupEnabled: false, // Group DMs groupChannels: [] // Specific group DM channels },
// Server and channel permissions allowedServers: ["123456789012345678"], // Server IDs allowedChannels: ["123456789012345678"], // Channel IDs blockedServers: [], blockedChannels: [],
// Message settings messages: { maxMessageLength: 2000, embeds: true, reactions: true, typingIndicator: true, mentionEveryone: false } } }}Advanced Settings
Section titled “Advanced Settings”{ channels: { discord: { // Activity and presence presence: { status: "online", // "online", "idle", "dnd", "invisible" activities: [ { name: "OpenClaw Assistant", type: "PLAYING" // "PLAYING", "STREAMING", "LISTENING", "WATCHING", "COMPETING" } ] },
// Slash commands commands: { enabled: true, commands: { "help": { description: "Show available commands", options: [] }, "status": { description: "Check bot status", options: [] }, "ask": { description: "Ask OpenClaw a question", options: [ { name: "question", description: "Your question for OpenClaw", type: "STRING", required: true } ] } } },
// Message handling messageHandling: { respondToBots: false, respondToEdits: false, deleteCommandMessages: false, errorReplyType: "reply" // "reply", "dm", "none" },
// Embed settings embeds: { color: 0x00ff00, // Green color author: { name: "OpenClaw", icon_url: "https://example.com/avatar.png" }, footer: { text: "Powered by OpenClaw", icon_url: "https://example.com/icon.png" } },
// Rate limiting rateLimit: { enabled: true, maxMessages: 5, windowMs: 5000, skipSuccessfulRequests: false } } }}Features
Section titled “Features”Direct Messages
Section titled “Direct Messages”- Secure pairing: Pairing system for unknown users
- Session management: Persistent conversation context
- Rich formatting: Markdown and embed support
- File sharing: Send and receive files
Server Channels
Section titled “Server Channels”- Channel-specific responses: Different behavior per channel
- Mention-based activation: Respond only when mentioned
- Role-based permissions: Control access by Discord roles
- Server-specific settings: Custom configuration per server
Slash Commands
Section titled “Slash Commands”- Built-in commands:
/help,/status,/ask - Custom commands: Define your own slash commands
- Command options: Rich command interfaces with parameters
- Auto-registration: Commands auto-register with Discord
Rich Interactions
Section titled “Rich Interactions”- Embeds: Rich message formatting with embeds
- Reactions: Add reactions to messages
- Buttons: Interactive button components
- Select menus: Dropdown selection interfaces
Usage Examples
Section titled “Usage Examples”Basic Commands
Section titled “Basic Commands”# Set Discord bot tokenopenclaw config set channels.discord.token "YOUR_BOT_TOKEN_HERE"
# Enable specific serveropenclaw config set channels.discord.allowedServers '["123456789012345678"]'
# Test Discord connectionopenclaw channels test discord
# Send a direct messageopenclaw message send --channel discord --target 123456789012345678 --message "Hello from OpenClaw!"
# Send to server channelopenclaw message send --channel discord --target 123456789012345678 --message "Hello everyone!" --server 987654321098765432Interactive Usage
Section titled “Interactive Usage”Once configured, users can interact with your bot:
User: /helpBot: 🤖 **OpenClaw Commands**/help - Show this help message/status - Check bot status/ask <question> - Ask OpenClaw a question/code <code> - Execute code
User: /ask What's the weather like today?Bot: 🌤️ I don't have access to real-time weather data, but I can help you create a weather checker! Would you like me to write a script using a weather API?
User: @OpenClaw Write a Python function to calculate factorialBot: 🐍 Here's a Python function to calculate factorial:
```pythondef factorial(n): """Calculate factorial of a non-negative integer.""" if n < 0: raise ValueError("Factorial is not defined for negative numbers") if n == 0 or n == 1: return 1 return n * factorial(n - 1)
# Test the functionfor i in range(6): print(f"{i}! = {factorial(i)}")Output:
0! = 11! = 12! = 23! = 64! = 245! = 120User: /status Bot: 📊 Bot Status
- Status: 🟢 Online
- Uptime: 2h 34m
- Servers: 3
- Channels: 12
- Messages processed: 247
- Active users: 45
## Slash Commands
### Registering Commands
Commands are automatically registered when the bot starts. You can also register them manually:
```bash# Register all commandsopenclaw discord commands register
# Register specific commandsopenclaw discord commands register --command helpopenclaw discord commands register --command status
# View registered commandsopenclaw discord commands listCustom Commands
Section titled “Custom Commands”Create custom slash command handlers:
module.exports = { slashCommands: { weather: async (interaction) => { const location = interaction.options.getString('location');
// Fetch weather data const weather = await getWeather(location);
const embed = { title: `Weather in ${location}`, description: `${weather.temp}°C, ${weather.description}`, color: 0x3498db, fields: [ { name: 'Humidity', value: `${weather.humidity}%`, inline: true }, { name: 'Wind Speed', value: `${weather.wind} km/h`, inline: true } ], timestamp: new Date().toISOString() };
await interaction.reply({ embeds: [embed] }); },
code: async (interaction) => { const code = interaction.options.getString('code'); const language = interaction.options.getString('language') || 'python';
try { const result = await executeCode(code, language);
const embed = { title: 'Code Execution Result', fields: [ { name: 'Input', value: `\`\`\`${language}\n${code}\`\`\`` }, { name: 'Output', value: `\`\`\`\n${result}\`\`\`` } ], color: 0x00ff00 };
await interaction.reply({ embeds: [embed] }); } catch (error) { await interaction.reply({ content: `❌ Error: ${error.message}`, ephemeral: true }); } } }};Advanced Features
Section titled “Advanced Features”Component Interactions
Section titled “Component Interactions”Add interactive buttons and select menus:
// Send message with buttonsawait interaction.reply({ content: 'Choose an option:', components: [ { type: 'ACTION_ROW', components: [ { type: 'BUTTON', customId: 'help', label: 'Help', style: 'PRIMARY' }, { type: 'BUTTON', customId: 'status', label: 'Status', style: 'SECONDARY' } ] } ]});
// Handle button interactionsmodule.exports = { onInteraction: async (interaction) => { if (interaction.isButton()) { switch (interaction.customId) { case 'help': await interaction.update({ content: 'Here\'s help information...' }); break; case 'status': await interaction.update({ content: 'Bot status information...' }); break; } } }};Voice Channel Support
Section titled “Voice Channel Support”Enable voice channel features:
{ channels: { discord: { voice: { enabled: true, autoJoin: false, joinChannels: ["123456789012345678"], // Voice channel IDs autoDisconnect: true, maxSilenceTime: 300000 // 5 minutes } } }}Webhooks and Integrations
Section titled “Webhooks and Integrations”Configure Discord webhooks:
{ channels: { discord: { webhooks: { onMessage: { url: "https://your-api.com/webhook/discord", secret: "webhook-secret", events: ["message", "interaction"] }, onJoin: { url: "https://your-api.com/webhook/join", events: ["guildJoin", "guildMemberAdd"] } } } }}Security Considerations
Section titled “Security Considerations”Bot Token Security
Section titled “Bot Token Security”- Environment variables: Store tokens in environment variables
- File permissions: Restrict access to config files
- Token rotation: Regularly rotate bot tokens
- Access monitoring: Monitor token usage patterns
Server Security
Section titled “Server Security”- Server whitelisting: Only allow specific servers
- Channel permissions: Restrict bot to specific channels
- Role-based access: Use Discord roles for permissions
- Audit logging: Log all bot actions
Content Security
Section titled “Content Security”- Input validation: Validate all user inputs
- Content filtering: Filter inappropriate content
- Rate limiting: Prevent spam and abuse
- Command sanitization: Sanitize command inputs
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Bot Not Responding
# Check bot tokenopenclaw config get channels.discord.token
# Test Discord connectionopenclaw channels test discord
# Check gateway logsopenclaw gateway logs --filter discord
# Verify bot permissions in Discord serverCommands Not Working
# Check command registrationopenclaw discord commands list
# Re-register commandsopenclaw discord commands register --force
# Check intents configurationopenclaw config get channels.discord.intentsPermission Issues
# Check allowed serversopenclaw config get channels.discord.allowedServers
# Verify bot has required permissions# In Discord: Server Settings → Roles → Bot Role → Permissions
# Test channel accessopenclaw channels test discord --channel 123456789012345678Rate Limiting
# Check rate limit statusopenclaw channels status discord --rate-limit
# Adjust rate limitsopenclaw config set channels.discord.rateLimit.maxMessages 10
# Monitor API usageopenclaw channels metrics discordDebug Mode
Section titled “Debug Mode”Enable detailed logging for Discord:
# Enable debug loggingopenclaw config set logging.components.discord debug
# Restart with debugopenclaw gateway restart --debug
# View Discord logsopenclaw gateway logs --follow --filter discordBest Practices
Section titled “Best Practices”-
Bot Design
- Clear command structure
- Helpful error messages
- Consistent user experience
- Accessibility considerations
-
Performance
- Implement proper caching
- Use efficient data structures
- Monitor API usage
- Optimize database queries
-
Security
- Validate all inputs
- Use HTTPS for webhooks
- Implement rate limiting
- Regular security audits
-
User Experience
- Use embeds for rich content
- Implement typing indicators
- Provide clear feedback
- Handle errors gracefully
API Reference
Section titled “API Reference”Channel Commands
Section titled “Channel Commands”# Discord channel managementopenclaw channels test discordopenclaw channels status discord [--detailed]openclaw discord commands register [--force]openclaw discord commands list
# Message operationsopenclaw message send --channel discord --target 123456789012345678 --message "Hello"openclaw message send --channel discord --target 123456789012345678 --file ./image.pngopenclaw message broadcast --channel discord --server 987654321098765432 --message "Announcement"
# Configurationopenclaw config set channels.discord.token "your-token"openclaw config set channels.discord.dm.policy "pairing"openclaw config get channels.discordConfiguration Schema
Section titled “Configuration Schema”interface DiscordConfig { enabled: boolean; token: string; intents?: string[]; dm?: { policy: "pairing" | "allowlist" | "denylist" | "open"; allowFrom?: string[]; blockFrom?: string[]; groupEnabled?: boolean; groupChannels?: string[]; }; allowedServers?: string[]; allowedChannels?: string[]; blockedServers?: string[]; blockedChannels?: string[]; messages?: { maxMessageLength?: number; embeds?: boolean; reactions?: boolean; typingIndicator?: boolean; mentionEveryone?: boolean; }; presence?: { status: "online" | "idle" | "dnd" | "invisible"; activities?: Array<{ name: string; type: "PLAYING" | "STREAMING" | "LISTENING" | "WATCHING" | "COMPETING"; }>; }; commands?: { enabled: boolean; commands?: { [command: string]: SlashCommand }; }; voice?: { enabled: boolean; autoJoin?: boolean; joinChannels?: string[]; autoDisconnect?: boolean; maxSilenceTime?: number; }; rateLimit?: { enabled: boolean; maxMessages: number; windowMs: number; skipSuccessfulRequests?: boolean; };}Your Discord bot is now ready! You can start building powerful Discord experiences with OpenClaw. 🎮