Discord servers benefit enormously from OpenClaw integration. Moltbot handles moderation, answers questions, and engages community members 24/7. This comprehensive guide walks through complete Discord setup with code examples.
Prerequisites
- OpenClaw CLI installed (
npm install -g openclaw-cli) - Discord account with server admin permissions
- Node.js 16+ and npm 8+
- Basic JavaScript knowledge (helpful but not required)
1Create Discord Application
Navigate to the Discord Developer Portal and create your bot application:
- Go to discord.com/developers/applications
- Click "New Application"
- Name your application (e.g., "Community Assistant")
- Navigate to the "Bot" tab
- Click "Add Bot" and confirm
2Configure Bot Permissions
Set up the required permissions for OpenClaw to function properly:
Required Intents (in Bot settings):
- ✓ Presence Intent
- ✓ Server Members Intent
- ✓ Message Content Intent
Bot Permissions (in OAuth2 → URL Generator):
Permissions needed:
- Read Messages/View Channels
- Send Messages
- Send Messages in Threads
- Embed Links
- Attach Files
- Read Message History
- Add Reactions
- Use Slash Commands
3Initialize OpenClaw for Discord
Create a new OpenClaw project configured for Discord:
openclaw init discord-bot --platform discord
cd discord-bot
This generates a project structure:
discord-bot/
├── openclaw.config.js
├── .env.example
├── commands/
│ ├── help.js
│ └── info.js
├── events/
│ ├── messageCreate.js
│ └── ready.js
└── package.json
4Configure Environment Variables
Copy .env.example to .env and add your credentials:
# Discord Configuration
DISCORD_BOT_TOKEN=your_bot_token_here
DISCORD_CLIENT_ID=your_client_id
DISCORD_GUILD_ID=your_server_id
# OpenClaw Settings
OPENCLAW_API_KEY=your_openclaw_api_key
OPENCLAW_MODE=production
OPENCLAW_LANGUAGE=en
# Optional: Advanced Features
ENABLE_LEARNING=true
ENABLE_MODERATION=true
LOG_LEVEL=info
- Bot Token: Discord Developer Portal → Your App → Bot → Token
- Client ID: Discord Developer Portal → Your App → OAuth2 → Client ID
- Guild ID: Enable Developer Mode in Discord → Right-click your server → Copy ID
5Customize Bot Configuration
Edit openclaw.config.js to define your bot's personality and capabilities:
module.exports = {
name: 'Community Assistant',
prefix: '!',
// AI Behavior
ai: {
model: 'openclaw-v2',
temperature: 0.7,
maxTokens: 500,
contextWindow: 10
},
// Discord-specific settings
discord: {
status: 'online',
activity: {
type: 'WATCHING',
name: 'for questions'
},
embedColor: '#5865F2'
},
// Feature toggles
features: {
autoModeration: true,
welcomeMessages: true,
customCommands: true,
reactionRoles: false
},
// Response templates
responses: {
greeting: 'Hey there! 👋 How can I help you today?',
farewell: 'Goodbye! Feel free to ask if you need anything.',
error: 'Oops! Something went wrong. Please try again.',
noPermission: 'Sorry, you don\'t have permission for that.'
}
};
6Create Custom Commands
Add a custom command in commands/server-info.js:
module.exports = {
name: 'serverinfo',
description: 'Display server information',
async execute(message, args, client) {
const { guild } = message;
const embed = {
color: 0x5865F2,
title: `📊 ${guild.name} Server Info`,
fields: [
{
name: 'Members',
value: `${guild.memberCount}`,
inline: true
},
{
name: 'Created',
value: guild.createdAt.toDateString(),
inline: true
},
{
name: 'Owner',
value: `<@${guild.ownerId}>`,
inline: true
}
],
timestamp: new Date()
};
await message.reply({ embeds: [embed] });
}
};
7Invite Bot to Your Server
Generate an invite URL with proper permissions:
openclaw invite --permissions 8
Or manually construct the URL:
https://discord.com/api/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=8&scope=bot%20applications.commands
Open the URL in your browser and select your server.
8Launch Your Bot
Start OpenClaw in development mode for testing:
openclaw dev
Expected output:
🚀 OpenClaw Discord Bot Starting...
✓ Configuration loaded
✓ Commands registered: 5
✓ Events loaded: 3
✓ Connected to Discord Gateway
✓ Bot online: Community Assistant#1234
✓ Serving 1 guild with 247 members
→ Ready to receive messages!
!help in your Discord server.
Advanced Configuration
Slash Commands
Register slash commands for better user experience:
// commands/slash/ping.js
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Check bot latency'),
async execute(interaction) {
const latency = Date.now() - interaction.createdTimestamp;
await interaction.reply(`🏓 Pong! Latency: ${latency}ms`);
}
};
Deploy slash commands:
openclaw deploy-commands
Event Handlers
Handle Discord events in events/messageCreate.js:
module.exports = {
name: 'messageCreate',
async execute(message, client) {
// Ignore bots
if (message.author.bot) return;
// Check for mentions
if (message.mentions.has(client.user)) {
const response = await client.openclaw.generateResponse(
message.content,
message.author.id
);
await message.reply(response);
}
}
};
Production Deployment
Deploy to production using PM2 for process management:
# Install PM2
npm install -g pm2
# Start bot with PM2
pm2 start openclaw --name discord-bot -- start
# Enable auto-restart on system reboot
pm2 startup
pm2 save
# Monitor logs
pm2 logs discord-bot
Troubleshooting
Common issues and solutions:
- Bot doesn't respond: Check Message Content Intent is enabled
- Permission errors: Verify bot role is above managed roles
- Commands not working: Ensure prefix matches configuration
- Connection issues: Validate bot token in .env file
For additional support, visit the OpenClaw Discord community or check our troubleshooting guide.