ByteNoteByteNote

字节笔记本

2026年8月29日

用 Discord 搭一套 OpenClaw Agent 协作

API中转
¥120

想在一个 Discord 服务器里同时跑多个专用 Agent(写代码的、答支持的、写文案的),又不想给每个角色都建一套独立基础设施,OpenClaw 的 Discord 通道 + bindings 路由正好干这件事。消息从哪个频道来,就由配置决定交给哪个 Agent,模型自己选不了出口,回复也会回到原频道。

先搞清楚三块东西

OpenClaw 里别把这几个概念混在一起:

  • Channel:接入通道,这里是 discord
  • Account:同一通道下可以挂多个 Bot 账号(多 token)
  • Agent:独立的工作区 + 会话存储(真正的「大脑」)
  • Binding:把「某类入站流量」映射到某个 agentId

Binding 只负责选 Agent,不负责放行。消息能不能进门,还是看 pairing、allowlist、groupPolicy 这些通道侧规则。先把 Bot 接上、消息能进,再配路由。

接 Discord Bot

  1. Discord Developer Portal 新建 Application,打开 Bot,设好用户名。
  2. 在 Privileged Gateway Intents 里打开:
    • Message Content Intent(频道里正常读消息几乎必备)
    • Server Members Intent(角色路由、按名字解析 ID 时需要)
    • Presence Intent 可选
  3. Reset Token(第一次其实是生成 token),只保存在本机,别贴进聊天。
  4. OAuth2 URL Generator 勾选 botapplications.commands,再按需勾权限,把 Bot 邀进你的服务器。

环境变量方式(默认账号):

bash
export DISCORD_BOT_TOKEN="YOUR_BOT_TOKEN"

配置里也可以写成 SecretRef,例如从环境变量读:

json5
{
  channels: {
    discord: {
      enabled: true,
      token: { source: "env", provider: "default", id: "DISCORD_BOT_TOKEN" },
    },
  },
}

拿不到 Message Content Intent 时,把 channels.discord.intents.messageContent 设成 false,并保持频道 requireMention: true。这时 Bot 主要靠 @ 提及和私信工作,普通频道闲聊内容 Discord 不会下发。

最容易踩的坑:groupPolicy

默认经常是 allowlist。效果是:Bot 进了服务器,频道消息静默丢掉,不报错也不提醒。调试时先查这个。

需要放开频道消息时:

bash
openclaw config set channels.discord.accounts.<accountId>.groupPolicy open

或者按服务器 / 频道做更细的 allowlist。私服小团队用 open 最省事;对外公开服建议白名单 + 提及门控。

两种协作形态

1)一个 Bot,按频道分 Agent

同一个 token,#code-help 走代码审查 Agent,#support 走排障 Agent,#writing 走文案 Agent。适合人少、服务器结构清晰的场景。

先注册账号(若尚未接入):

bash
openclaw channels add --channel discord \
  --account default \
  --name "OpenClaw" \
  --token "$DISCORD_BOT_TOKEN"

再建几个 Agent,各自独立工作区:

bash
openclaw agents add coding --workspace ~/.openclaw/workspace-coding
openclaw agents add support --workspace ~/.openclaw/workspace-support
openclaw agents add writing --workspace ~/.openclaw/workspace-writing

~/.openclaw/openclaw.jsonbindings 里写频道级规则(窄规则放前面):

json5
{
  agents: {
    entries: {
      main: { default: true, workspace: "~/.openclaw/workspace" },
      coding: { workspace: "~/.openclaw/workspace-coding" },
      support: { workspace: "~/.openclaw/workspace-support" },
      writing: { workspace: "~/.openclaw/workspace-writing" },
    },
  },
  bindings: [
    {
      agentId: "coding",
      match: {
        channel: "discord",
        accountId: "default",
        peer: { kind: "channel", id: "111111111111111111" },
      },
    },
    {
      agentId: "support",
      match: {
        channel: "discord",
        accountId: "default",
        peer: { kind: "channel", id: "222222222222222222" },
      },
    },
    {
      agentId: "writing",
      match: {
        channel: "discord",
        accountId: "default",
        peer: { kind: "channel", id: "333333333333333333" },
      },
    },
  ],
}

频道 ID:Discord 设置里打开开发者模式,右键频道 → Copy Channel ID。也可以用:

bash
openclaw directory groups list --channel discord --account default

改完配置后重启 Gateway(路由一般不热更新):

bash
openclaw gateway restart
openclaw agents list --bindings
openclaw channels status --probe

2)多个 Bot,每个 Bot 绑一个 Agent

隔离更强:权限、mention、allowlist 按 Bot 拆开。适合「客服 Bot」和「内部研发 Bot」不能混会话的团队。

json5
{
  agents: {
    list: [
      { id: "main", workspace: "~/.openclaw/workspace-main" },
      { id: "coding", workspace: "~/.openclaw/workspace-coding" },
    ],
  },
  bindings: [
    { agentId: "main", match: { channel: "discord", accountId: "default" } },
    { agentId: "coding", match: { channel: "discord", accountId: "coding" } },
  ],
  channels: {
    discord: {
      groupPolicy: "allowlist",
      accounts: {
        default: {
          token: "DISCORD_BOT_TOKEN_MAIN",
          guilds: {
            "123456789012345678": {
              channels: {
                "222222222222222222": { allow: true, requireMention: false },
              },
            },
          },
        },
        coding: {
          token: "DISCORD_BOT_TOKEN_CODING",
          guilds: {
            "123456789012345678": {
              channels: {
                "333333333333333333": { allow: true, requireMention: false },
              },
            },
          },
        },
      },
    },
  },
}

每个 Bot 都要单独邀请进服务器,并各自打开 Message Content Intent。

路由优先级(记这一张表就够)

入站消息按特异性选 Agent,大致顺序:

  1. 精确 peer(某个频道 / 某个用户)
  2. 父 peer(线程继承)
  3. peer 通配
  4. Discord:guildId + roles
  5. Discord:整服 guildId
  6. Slack:teamId
  7. 具体 accountId
  8. 通道级回落(accountId: "*"
  9. 默认 Agent

同一档位里,配置里写在前面的先匹配。所以「频道规则」一定要放在「账号级兜底」前面,否则永远落到兜底 Agent。

按角色分流示例(管理员走 admin Agent):

json5
{
  agentId: "admin",
  match: {
    channel: "discord",
    guildId: "123456789012345678",
    roles: ["987654321098765432"],
  },
}

注意:roles 用的是角色 ID,不是显示名;没开 Server Members Intent 时这类匹配容易失效。

会话怎么隔离

默认情况下:

  • 私信会收拢到该 Agent 的 main session(可用 session.dmScope 改成按人拆)
  • 频道 / 群默认 per-group,彼此上下文不串
  • Discord / Slack 线程会在 session key 里追加 :thread:<id>

如果希望某几个频道共享同一段上下文,可以在对应 binding 上设 session.groupScope: "main"。这只改共享上下文,回复仍然回到消息来源频道。

调试清单

Bot 完全不说话:

  • Message Content Intent 是否已开
  • groupPolicy 是否把消息挡掉了
  • 频道是否要求 @提及 才响应
  • token 是否对应你以为的那个 account

所有频道人设一模一样:

  • bindings 顺序是否被账号级规则抢先匹配
  • agentId 是否真的存在于 agents.entries / agents.list
  • 改完配置后有没有 gateway restart

想确认当前映射:

bash
openclaw agents list --bindings
openclaw channels status --probe

相关文档

先用一个私服、两个频道、两个 Agent 跑通「消息进得来、路由分得开、回复回得去」,再往角色路由和多 Bot 隔离加复杂度,会稳很多。

分享: