OpenClaw Discord 集成:构建智能社区机器人

Discord 作为全球领先的游戏社区和开发者社区平台,拥有数亿用户。通过 OpenClaw 的 Discord 集成,你可以创建一个强大的社区机器人,自动处理各种任务、提供服务、维护社区秩序。本文将详细介绍如何配置和优化 OpenClaw 的 Discord 功能。

Discord Bot 基础

为什么选择 Discord?

Discord 的核心优势:

  • 强大的社区功能:支持文本、语音、视频频道
  • 丰富的权限系统:细粒度的角色和权限管理
  • 开发者友好:完善的 API 和 SDK
  • 广泛的用户群体:游戏、开发者、创作者等多元化社区
  • 免费且功能完整:核心功能免费使用

Bot 类型选择

Discord Bot 有两种主要类型:

  1. Bot 应用:独立机器人账户
  2. 用户 Bot(已废弃):使用普通用户账户

本文专注于 Bot 应用,这是目前官方推荐的方式。

创建 Discord Bot

步骤 1:创建 Discord 应用

  1. 访问 Discord Developer Portal
  2. 点击 "New Application"
  3. 输入应用名称(如 "OpenClaw Assistant")
  4. 点击 "Create"

步骤 2:创建 Bot 用户

  1. 在左侧菜单选择 "Bot"
  2. 点击 "Add Bot"
  3. 确认创建
  4. 重要:复制并保存 Bot Token(只显示一次)

步骤 3:配置 Intent

在 Bot 设置中,找到 "Privileged Gateway Intents"

  • PRESENCE INTENT:获取用户状态信息
  • SERVER MEMBERS INTENT:获取服务器成员列表
  • MESSAGE CONTENT INTENT:读取消息内容(重要!)

注意:如果要在超过 100 个服务器的 Bot 上启用,需要向 Discord 申请验证。

步骤 4:邀请 Bot 到服务器

  1. 在左侧菜单选择 "OAuth2" → "URL Generator"
  2. 选择 Scopes
  3. ✅ bot
  4. ✅ applications.commands
  5. 选择 Bot Permissions
  6. ✅ Send Messages
  7. ✅ Embed Links
  8. ✅ Attach Files
  9. ✅ Read Message History
  10. ✅ Use Slash Commands
  11. ✅ Manage Channels(可选)
  12. ✅ Manage Roles(可选)
  13. 复制生成的 URL
  14. 在浏览器中打开 URL,选择服务器,授权 Bot

OpenClaw Discord 配置

环境变量

.env 文件中添加:

# Discord Bot Token(必填)
DISCORD_BOT_TOKEN=MTIzNDU2Nzg5MDEyMzQ1Njc4.GaBcDe.FgHiJkLmNoPqRsTuVwXyZ012345678901234567890

# 应用 ID(可选,用于 Slash 命令)
DISCORD_APP_ID=123456789012345678

# 允许的服务器 ID(可选)
DISCORD_GUILD_IDS=123456789012345678,987654321098765432

# 允许的频道 ID(可选)
DISCORD_CHANNEL_IDS=111222333444555666,777888999000111222

# 管理员用户 ID(可选)
DISCORD_ADMIN_USERS=123456789012345678

基础配置

config/app.yaml 中配置:

plugins:
  - discord

discord:
  enabled: true
  botToken: "${DISCORD_BOT_TOKEN}"

  # 基础设置
  settings:
    # 命令前缀(用于文本命令)
    commandPrefix: "!"
    # 是否响应自己的消息
    respondToSelf: false
    # 是否响应其他机器人
    respondToBots: false
    # 最大嵌入长度
    maxEmbedLength: 6000

  # 服务器权限
  permissions:
    # 允许的服务器(留空则允许所有)
    allowedGuilds: []
    # 允许的频道(留空则允许所有)
    allowedChannels: []
    # 管理员命令
    adminCommands:
      - "admin"
      - "reload"
      - "restart"
      - "config"

  # 功能开关
  features:
    # Slash 命令
    slashCommands: true
    # 技能系统
    skills: true
    # 定时任务
    cron: true
    # 记忆功能
    memory: true
    # 消息监听
    messageListener: true

Slash 命令配置

创建 config/discord/commands.yaml

commands:
  - name: "help"
    description: "获取帮助信息"
    type: 1  # CHAT_INPUT

  - name: "search"
    description: "搜索信息"
    type: 1
    options:
      - name: "query"
        description: "搜索关键词"
        type: 3  # STRING
        required: true

  - name: "weather"
    description: "查询天气"
    type: 1
    options:
      - name: "city"
        description: "城市名称"
        type: 3
        required: true

服务器特定配置

discord:
  guilds:
    # 开发服务器
    - guildId: "123456789012345678"
      name: "开发团队"
      features:
        skills: true
        slashCommands: true
      allowedSkills:
        - "code-helper"
        - "search"
        - "documentation"
      channels:
        - "bot-commands"
        - "general"

    # 社区服务器
    - guildId: "987654321098765432"
      name: "OpenClaw 社区"
      features:
        skills: true
        memory: false  # 社区关闭记忆
      allowedSkills:
        - "faq"
        - "help"

使用场景示例

场景 1:技术社区问答机器人

discord:
  enabled: true
  botToken: "${DISCORD_BOT_TOKEN}"
  features:
    skills: true

skills:
  - path: "./skills/stackoverflow"
    name: "技术问答"
    triggers:
      - "问:"
      - "如何"

  - path: "./skills/code-review"
    name: "代码审查"
    triggers:
      - "review:"

场景 2:社区管理机器人

discord:
  features:
    moderation: true

moderation:
  # 自动欢迎新成员
  welcome:
    enabled: true
    message: "欢迎 {user} 加入 {guild}!请阅读 #rules 频道"
    channel: "welcome"

  # 自动角色分配
  autoRole:
    enabled: true
    roles:
      - "Member"

  # 违规词过滤
  filter:
    enabled: true
    words:
      - "bad-word"
    action: "delete"

场景 3:游戏服务器助手

discord:
  features:
    slashCommands: true

commands:
  - name: "server"
    description: "查询服务器状态"

  - name: "player"
    description: "查询玩家信息"
    options:
      - name: "name"
        type: 3
        required: true

高级功能

1. Embed 消息

创建富文本消息:

const embed = {
  color: 0x0099ff,
  title: 'OpenClaw 状态报告',
  url: 'https://example.com/report',
  author: {
    name: 'OpenClaw Bot',
    icon_url: 'https://example.com/icon.png',
  },
  description: '系统运行状态正常',
  fields: [
    {
      name: 'CPU 使用率',
      value: '25%',
      inline: true,
    },
    {
      name: '内存使用',
      value: '4GB / 8GB',
      inline: true,
    },
  ],
  timestamp: new Date().toISOString(),
  footer: {
    text: 'OpenClaw Monitor',
  },
};

channel.send({ embeds: [embed] });

2. 按钮交互

const row = new ActionRowBuilder()
  .addComponents(
    new ButtonBuilder()
      .setCustomId('primary')
      .setLabel('确认')
      .setStyle(ButtonStyle.Primary),
    new ButtonBuilder()
      .setCustomId('secondary')
      .setLabel('取消')
      .setStyle(ButtonStyle.Secondary),
  );

channel.send({ content: '请选择:', components: [row] });

3. 选择菜单

const row = new ActionRowBuilder()
  .addComponents(
    new StringSelectMenuBuilder()
      .setCustomId('select')
      .setPlaceholder('选择一个选项')
      .addOptions([
        {
          label: '选项 1',
          description: '这是选项 1 的描述',
          value: 'option1',
        },
        {
          label: '选项 2',
          description: '这是选项 2 的描述',
          value: 'option2',
        },
      ]),
  );

4. 模态对话框

const modal = new ModalBuilder()
  .setCustomId('myModal')
  .setTitle('提交表单');

const input = new TextInputBuilder()
  .setCustomId('textInput')
  .setLabel("输入内容")
  .setStyle(TextInputStyle.Paragraph);

modal.addComponents(new ActionRowBuilder().addComponents(input));

常用命令示例

文本命令

!help          - 获取帮助
!status        - 查看状态
!ping          - 测试延迟
!weather 北京   - 查询天气
!search 关键词 - 搜索信息

Slash 命令

/help          - 获取帮助
/search query  - 搜索
/weather city  - 天气查询
/config        - 配置管理

性能优化

1. 消息缓存

discord:
  cache:
    messages: 100
    channels: 50
    guilds: 10

2. 分片处理

大型服务器使用分片:

discord:
  shards:
    enabled: true
    count: auto

3. 延迟优化

discord:
  performance:
    # 批量处理消息
    batchProcess: true
    batchSize: 10
    # 响应延迟(毫秒)
    responseDelay: 100

常见问题

问题 1:Bot 不响应命令

检查: 1. Bot 是否有 Message Content Intent 2. Bot 是否有发送消息权限 3. 检查服务器/频道是否在允许列表 4. 查看控制台日志

问题 2:Slash 命令不显示

解决方案: 1. 确保已注册 Slash 命令 2. 等待 Discord 同步(最多 1 小时) 3. 使用开发模式命令(仅特定服务器生效快)

问题 3:权限不足

在邀请 Bot 时确保选择了正确的权限,或在服务器设置中调整 Bot 角色权限。

问题 4:Bot 掉线

可能原因: - 网络连接问题 - Token 过期 - Discord API 限流

安全建议

  1. 最小权限原则:只授予必要的权限
  2. Token 保护:不要将 Token 提交到代码库
  3. 用户验证:敏感操作前验证用户身份
  4. 日志审计:记录重要操作日志
  5. 限流保护:配置命令使用频率限制

总结

通过本文,你应该已经掌握了 OpenClaw Discord 集成的完整配置方法。Discord 机器人可以成为社区管理的利器,大幅提升运营效率和用户体验。

关键要点: - 正确配置 Intent 权限 - 合理设置服务器和频道访问控制 - 充分利用 Slash 命令和交互组件 - 持续优化性能和安全性

祝你的 Discord Bot 运行顺利!


相关阅读: - OpenClaw Telegram 集成 - OpenClaw 技能开发 - OpenClaw 自动化工作流