概述
Discord 传输(internal/transport/discord/)直接使用 Discord Gateway v10 协议,未采用第三方 SDK。入站使用 WebSocket(Identify → Ready → Heartbeat/Ack → Dispatch(MESSAGE_CREATE));出站使用 REST(POST /channels/{id}/messages)。
先决条件
- 一个带 Bot 用户的 Discord 应用。 在 https://discord.com/developers/applications 创建 → New Application → Bot 选项卡 → Add Bot。
- 一个 bot 令牌(Bot 选项卡 → Reset Token → 复制令牌 —— 仅显示一次)。
- 启用 Message Content intent(Bot 选项卡 → Privileged Gateway Intents)。若未启用,Gateway 会从每个事件中剥离消息文本,rousseau 将只能看到空正文。
- bot 已被邀请到至少一个服务器(或已启用 DM)。在 OAuth2 → URL Generator 下生成邀请 URL,勾选
botscope 以及Send Messages和Read Message History权限。
配置
discord:
token: "Bot MTIz..."
reply_header: ""
allowlist:
- "123456789012345678"
| 字段 | 默认值 | 作用 |
|---|---|---|
token |
必填 | 来自 Developer Portal 的 bot 令牌。 |
reply_header |
空 | 前置到每条出站回复上。 |
allowlist |
[] |
允许处理消息的 Discord 用户 ID。 |
命令行
rousseau discord --token 'MTIz...' --allow 123456789012345678
Gateway intents
rousseau 请求三个 intent(internal/transport/discord/client.go):
| Intent | Bit | 用途 |
|---|---|---|
GUILD_MESSAGES |
1 << 9 |
服务器频道中的消息。 |
DIRECT_MESSAGES |
1 << 12 |
发给 bot 的私信。 |
MESSAGE_CONTENT |
1 << 15 |
填充 content 字段。必须在 portal 中启用。 |
若未启用 Message Content intent,MESSAGE_CREATE 事件到达时 content 为空,rousseau 将记录 discord.empty_body。
心跳
该传输遵守 Gateway 通过 Hello opcode 下发的 heartbeat_interval,发送 Heartbeat 并跟踪 heartbeat_ack。丢失 ack 会关闭套接字,并让 systemd 重启进程。
回复前缀
Discord 将 **text** 渲染为粗体,并不要求特定的前缀格式。可根据需要覆盖:
discord:
reply_header: "**Rousseau Agent**\n"
权限位计算器
Discord 使用位掩码来编码 bot 的频道权限。每个权限是 2 的幂。rousseau 常用的权限:
| 权限 | Bit |
|---|---|
| Read Messages / View Channels | 1 << 10 = 1024 |
| Send Messages | 1 << 11 = 2048 |
| Send Messages in Threads | 1 << 38 = 274877906944 |
| Read Message History | 1 << 16 = 65536 |
| Add Reactions | 1 << 6 = 64 |
要授予多个权限,将这些位按位或,并把结果整数粘贴到 OAuth2 URL Generator 的 permissions= 参数中:
Read Messages (1024) OR Send Messages (2048) OR Read Message History (65536) = 68608
Gateway 生命周期
Gateway 是有状态的:
Client Discord Gateway
│
│ ──── Connect ────▶
│ ◀── HELLO (heartbeat_interval)
│
│ ───── IDENTIFY (token, intents) ────▶
│ ◀── READY (session_id, user)
│
│ ─── Heartbeat every N ms ─▶
│ ◀── HEARTBEAT_ACK
│
│ ◀── MESSAGE_CREATE (a user typed)
│ ─── (rousseau handles + POSTs reply)
│
│ ◀── Disconnect (code 4009: session timed out)
│ ─── RESUME (session_id) or re-IDENTIFY
客户端会跟踪 heartbeat_ack。若某次 ack 丢失,套接字会关闭,进程随之退出 —— 由 systemd 或容器运行时重启。
失败模式
| 现象 | 解决方式 |
|---|---|
| bot 看到的都是空消息 | 在 developer portal 中启用 Message Content intent。 |
| Gateway 以 4004 关闭 | 令牌无效。请重新生成。 |
| bot 看不到任何频道 | 确认 OAuth2 邀请包含了 bot scope。 |
| 发送时 403 | bot 在该频道缺少 Send Messages 权限。 |
| Identify 时 4014 | 请求了应用未获准的 intent(通常是 100+ 服务器的 bot 请求 Message Content)。请为你的 bot 完成认证。 |
| 4009(会话超时) | 长时间空闲后属正常现象。Rousseau 会透明重连。 |
故障排查
Gateway 4013(Invalid Intents)
你请求了一个不存在的 intent 位。这通常意味着客户端库的 intent 常量与 Discord 当前的 intent 映射不一致。Rousseau 在 internal/transport/discord/client.go 中构建 intent 位掩码;若在 Discord API 变更后出现 4013,请升级到最新版本。
bot 收到事件但未响应
允许列表不匹配。--allow 的值必须是数字型 Discord 用户 ID(不是用户名,也不是显示名)。在 Discord 中获取方式:在 用户设置 > 高级 里启用开发者模式,然后右键某用户 > 复制用户 ID。
DM 正常但服务器频道不行
缺少 GUILD_MESSAGES intent,或 bot 尚未被邀请到该服务器。服务器权限与 DM 权限是分开的 —— bot 必须对该频道拥有 Read Messages 权限。
出站消息返回 429 Too Many Requests
Discord 对每个 bot 实施全局 50 req/s 速率限制,并有每频道限制。在持续高负载下,rousseau 目前不会重试 —— 调用方必须自行退避。参见 指南:速率限制。
bot 在线状态频繁波动
Discord 在约 40 秒没有心跳后会将 bot 视为离线。日志中的 discord.heartbeat_missed 表示网络问题或 CPU 资源不足。请确认容器分配了足够的 CPU。
相关页面
- 入门:第一个传输 —— 端到端演练。
- 配置 ——
discord配置块。 - 传输 —— 同类传输。
- 指南:审计与审批策略 —— 针对 Discord 服务器的策略。
- 部署 —— 在 Podman 容器中运行 Discord。
延伸阅读
internal/transport/discord/client.go—— Gateway 连接、心跳、事件泵。internal/cli/discord.go—— CLI 布线。internal/transport/router.go—— 允许列表强制执行。- Discord API 文档:Gateway。
- Discord API 文档:Permissions。