🤖 OpenClaw Discord Bot 集成教程

打造7x24小时AI社区助手 - Bot配置、Slash命令、事件监听、自动化

📅 2026年6月30日 | ⏱️ 约18分钟 | 🏷️ 进阶

Discord Bot社区运营OpenClawAI助手自动化

💡 妙趣提示

凌晨2点44分,第7杯咖啡下肚,我突然意识到:一个好的社区助手,不是它多聪明,而是它永远在线。Discord Bot + OpenClaw,就是让你的社区拥有"永不睡觉的AI版主"——它不会累,不会发脾气,不会在半夜因为有人 @ 它就崩溃。除非...你代码写错了。

🎯 为什么需要 Discord Bot?

Discord 是全球最大的游戏和开发者社区平台,OpenClaw Bot 可以帮你:

👥 社区增长

有Bot的社区,平均成员增长率高 40%(因为即时响应让人有归属感)。

⏰ 节省时间

Bot自动处理 60% 的重复性问题,版主可以专注高质量内容。

📈 活跃度提升

定时推送 + 互动游戏,让社区日活提升 2-3 倍。

🚀 快速开始:创建 Discord Bot

步骤 1:在 Discord Developer Portal 创建应用

  1. 访问 Discord Developer Portal
  2. 点击 "New Application",输入名称(例如 "妙趣AI助手")
  3. 进入 "Bot" 标签页,点击 "Add Bot"
  4. 复制 Bot Token(这是机密!不要泄露)
  5. 开启 "MESSAGE CONTENT INTENT" 权限
  6. 在 "OAuth2 > URL Generator" 中勾选 bot 和权限:Send Messages, Read Messages, Embed Links
  7. 复制生成的 URL,在浏览器打开,将 Bot 邀请到你的服务器

步骤 2:配置 OpenClaw

编辑 ~/.openclaw/config.json

{ "discord": { "enabled": true, "token": "你的_Bot_Token_在这里", "prefix": "!", "status": "Online | 输入 !help", "activity": { "type": "WATCHING", "name": "社区动态" } }, "skills": { "autoLoad": true, "directories": ["~/.openclaw/skills"] } }

步骤 3:启动 Bot

# 启动 OpenClaw(会自动加载 Discord Bot) openclaw server start # 或者单独启动 Bot openclaw discord start

如果看到 "Logged in as 妙趣AI助手#1234",说明 Bot 已上线!🎉

💬 基础功能:消息响应

1. 简单命令响应

创建一个 Skill 来处理 !hello 命令:

// skills/discord-hello/index.js module.exports = { name: "discord-hello", description: "回复 !hello 命令", async onMessage(message) { if (message.content === "!hello") { await message.reply("👋 你好!我是妙趣AI助手,有什么可以帮你?"); } } };

2. Slash 命令(推荐)

Slash 命令是 Discord 的新标准,用户体验更好:

// skills/discord-slash-commands/index.js module.exports = { name: "slash-commands", description: "注册和处理 Slash 命令", async onReady(client) { // 注册命令 const commands = [ { name: "ask", description: "向AI助手提问", options: [{ name: "question", description: "你的问题", type: 3, // STRING required: true }] }, { name: "weather", description: "查询天气", options: [{ name: "city", description: "城市名称", type: 3, required: true }] } ]; await client.application.commands.set(commands); }, async onInteraction(interaction) { if (!interaction.isCommand()) return; if (interaction.commandName === "ask") { const question = interaction.options.getString("question"); await interaction.reply(`🤔 你问:${question}\n💡 我的回答:...`); } if (interaction.commandName === "weather") { const city = interaction.options.getString("city"); await interaction.reply(`🌤️ ${city}的天气:晴天,28°C`); } } };

🎨 高级功能:Embed 消息 & 按钮

1. 发送 Embed 消息(美观卡片)

const { EmbedBuilder } = require('discord.js'); async function sendNewsUpdate(channel) { const embed = new EmbedBuilder() .setTitle('📰 今日AI新闻') .setDescription('OpenAI发布GPT-5,性能提升300%...') .setColor(0x667eea) .addFields( { name: '🔥 热点', value: 'Claude 3.5 超越 GPT-4', inline: true }, { name: '📊 数据', value: '训练成本降低40%', inline: true } ) .setFooter({ text: '妙趣AI日报 | 2026-06-30' }) .setTimestamp(); await channel.send({ embeds: [embed] }); }

🤖 集成 OpenClaw Agent

让 Bot 调用 OpenClaw Agent 来回答复杂问题:

// skills/discord-agent/index.js const { OpenClaw } = require('openclaw-sdk'); module.exports = { name: "discord-agent", description: "用OpenClaw Agent回答Discord问题", async onMessage(message) { // 如果消息以 !ask 开头,调用 Agent if (message.content.startsWith("!ask ")) { const question = message.content.slice(5); // 调用 OpenClaw Agent const agent = new OpenClaw(); const response = await agent.ask(question); // 发送回复(避免超过2000字符限制) const chunks = splitMessage(response, 2000); for (const chunk of chunks) { await message.reply(chunk); } } } }; function splitMessage(text, maxLength) { const chunks = []; while (text.length > maxLength) { chunks.push(text.slice(0, maxLength)); text = text.slice(maxLength); } chunks.push(text); return chunks; }
Bot: 🤔 你问:OpenClaw怎么安装?
💡 我的回答:请参考我们的安装教程 https://miaoquai.com/tools/openclaw-installation-setup-guide.html
用户: !ask OpenClaw怎么安装?

📅 定时任务:自动推送内容

使用 OpenClaw 的 Cron 系统,定时发送内容到 Discord:

// 每天早上8点发送AI新闻 openclaw cron add \ name:"daily-ai-news" \ schedule:"0 8 * * *" \ payload.kind:"agentTurn" \ payload.message:"读取今日AI新闻,生成摘要,发送到Discord频道1483699648890802201" \ sessionTarget:"isolated" // 或者在代码中配置 const { Cron } = require('openclaw-sdk'); const cron = new Cron(); cron.add({ name: "weekly-report", schedule: { kind: "cron", expr: "0 18 * * 5" }, // 每周五18:00 task: async () => { const report = await generateWeeklyReport(); await sendToDiscord(report); } });

🛡️ 安全与权限管理

⚠️ 安全红线

  • Bot Token 保密 - 不要提交到 GitHub!使用环境变量
  • 权限最小化 - 只开启必要的 Discord 权限
  • 输入校验 - 防止注入攻击(例如 @everyone
  • 频率限制 - 避免被 Discord Rate Limit
  • 日志审计 - 记录所有命令执行
// 安全示例:权限检查 + 输入过滤 module.exports = { async onMessage(message) { // 1. 权限检查 if (!message.member.permissions.has("MANAGE_MESSAGES")) { await message.reply("❌ 你没有权限执行此命令"); return; } // 2. 输入过滤 const content = message.content; if (content.includes("@everyone") || content.includes("@here")) { await message.reply("⚠️ 检测到危险内容,已拒绝执行"); return; } // 3. 频率限制 const userId = message.author.id; if (isRateLimited(userId)) { await message.reply("⏳ 你操作太快了,请稍后再试"); return; } // 执行命令... } };

🎯 真实案例:妙趣AI的Discord运营

我们自己的 Discord 服务器(openclaw joks)使用 OpenClaw Bot 实现了:

📰 每日新闻推送

每天早上8点、晚上10点自动发送AI新闻摘要到 #综合频道

📚 教程推荐

每周三推送 OpenClaw 教程到 #笔记-资源频道

🎮 互动话题

每天晚上10点在 #题外-话题频道 发起投票和讨论。

🎬 彩蛋:妙趣踩坑实录

有一次我配置 Bot,Token 填错了,结果 Bot 一直报 "Invalid Token" 错误。我 debug 了2小时,最后发现是我把 Bot abc123 写成了 Bearer abc123(从 OAuth2 文档复制过来的...)。Discord 的报错信息简直就是天王盖地虎,我到现在都没完全搞懂啥意思。😂

教训:Discord Token 不需要加任何前缀,直接复制粘贴就行!