Disclaw — Discord Structure as Code
Disclaw manages Discord workspace structure (categories, channels, threads) and OpenClaw agent-to-channel bindings as code via a YAML config file.
Disclaw vs Discord plugin: Disclaw manages structure (create/rename/delete channels, categories, threads; manage bindings and routing gates). The Discord plugin manages messaging (send/receive, reactions, pins, thread replies). They do not conflict.
Installation
CODEBLOCK0
Prerequisites
- 1. Discord bot token must be in OpenClaw config at INLINECODE0
- Gateway API access — add to
openclaw.json:
{ gateway: { tools: { allow: ["gateway"] } } }
This lets disclaw read/write config via the gateway API. Without it, disclaw falls back to CLI.
- 3. Config file — create
disclaw.yaml in the workspace (see format below)
Verify Setup
CODEBLOCK2
Config File Format
The config file (disclaw.yaml) declares the desired state of Discord workspace structure.
CODEBLOCK3
Key rules
- -
guild is the Discord server ID (right-click server → Copy Server ID) - Channel names must be lowercase, no spaces (Discord enforces this)
- Thread names can have spaces and mixed case
- Agent bindings reference channel names from the
channels section - INLINECODE6 controls whether the bot needs @mention to respond in that channel
Commands
disclaw diff — Show what would change
CODEBLOCK4
Shows: managed resources (create/update/delete/noop), unmanaged resources, unbound agents, stale agents, routing health warnings, and pinned messages.
disclaw apply — Apply changes (dry-run by default)
CODEBLOCK5
Safety: Always takes a snapshot before mutating. Creates before deletes. Bindings and routing gates are updated atomically.
disclaw import — Import unmanaged Discord resources
CODEBLOCK6
Discovers Discord channels/categories/threads not in the config and adds them. Also finds unbound OpenClaw agents.
disclaw rollback — Restore from snapshot
CODEBLOCK7
Restores Discord state from the most recent pre-apply snapshot. Drift-aware (shows what changed since the snapshot).
disclaw validate — Validate config (no API calls)
CODEBLOCK8
Safe for CI. Checks: schema validity, empty names, duplicate channels/threads, binding refs pointing to non-existent channels.
Filter flags (diff, apply, import)
CODEBLOCK9
Gateway options (all commands except validate)
CODEBLOCK10
Also via env vars: OPENCLAW_GATEWAY_URL, OPENCLAW_GATEWAY_TOKEN.
Common Workflows
Add a new channel
- 1. Edit
disclaw.yaml — add channel under the appropriate category - Validate: INLINECODE15
- Preview: INLINECODE16
- Apply: INLINECODE17
Bind an agent to a channel
- 1. Edit
disclaw.yaml — add entry under INLINECODE19 - Preview: INLINECODE20
- Apply: INLINECODE21
This creates the binding AND allowlists the channel in routing gates automatically.
Import existing Discord channels
- 1. Run:
disclaw import -c disclaw.yaml (dry-run to preview) - Review the output
- Run:
disclaw import -c disclaw.yaml --yes (writes to config) - Verify: INLINECODE24
Delete a channel
- 1. Remove the channel from INLINECODE25
- Preview: INLINECODE26
- Apply: INLINECODE27
Warning: --prune is required for deletions. Without it, removed channels are shown as "unmanaged" but not deleted.
Rename a channel
- 1. Change the channel name in INLINECODE29
- Preview:
disclaw diff -c disclaw.yaml (shows delete old + create new) - Apply: INLINECODE31
Note: Discord doesn't support true renames via API — disclaw creates the new channel and (with --prune) deletes the old one.
Troubleshooting
"Discord bot token not found"
- - Ensure
channels.discord.token is set in INLINECODE34 - Or set
DISCORD_BOT_TOKEN env var
"Gateway tool not available"
- - Add
gateway.tools.allow: ["gateway"] to INLINECODE37 - Or disclaw will fall back to CLI automatically
"Gateway auth failed"
- - Check
OPENCLAW_GATEWAY_TOKEN env var matches gateway.auth.token in config - Or pass INLINECODE40
"OpenClaw CLI timed out"
- - Check if the gateway is running: INLINECODE41
- Check network: INLINECODE42
"Discord connection timed out"
- - Check internet connectivity
- Verify bot token is valid
- Try again (Discord API can be flaky)
"Permission denied" during apply
- - Bot needs: Manage Channels, Manage Threads permissions
- Check bot role position in Discord server settings
Safety
- - Dry-run by default — all mutating commands require INLINECODE43
- Snapshot before apply — automatic, saved to INLINECODE44
- Rollback available — INLINECODE45
- Managed-scope only — disclaw only touches resources in the config
- Creates before deletes — safer ordering during apply
- Validation — config is validated before any API calls
Disclaw — Discord 结构即代码
Disclaw 通过 YAML 配置文件,以代码形式管理 Discord 工作区的结构(分类、频道、线程)以及 OpenClaw 代理与频道的绑定关系。
Disclaw 与 Discord 插件的区别: Disclaw 管理结构(创建/重命名/删除频道、分类、线程;管理绑定和路由网关)。Discord 插件管理消息(发送/接收、反应、置顶、线程回复)。两者互不冲突。
安装
bash
npm install -g @ofan/disclaw
disclaw --version
前提条件
- 1. Discord 机器人令牌 必须位于 OpenClaw 配置的 channels.discord.token 中
- 网关 API 访问权限 — 在 openclaw.json 中添加:
json5
{ gateway: { tools: { allow: [gateway] } } }
这允许 disclaw 通过网关 API 读写配置。若无此配置,disclaw 将回退到 CLI 模式。
- 3. 配置文件 — 在工作区中创建 disclaw.yaml(格式见下文)
验证设置
bash
disclaw validate -c disclaw.yaml
disclaw diff -c disclaw.yaml
配置文件格式
配置文件(disclaw.yaml)声明了 Discord 工作区结构的期望状态。
yaml
version: 1
managedBy: disclaw
guild: YOURGUILDID
channels:
# 独立频道(无分类)
- name: announcements
topic: 重要公告
# 带分类的频道
- category: 工程部
channels:
- name: general
threads: [站会, 回顾]
- name: alerts
topic: 仅限自动告警
# 另一个分类
- category: 支持部
channels:
- name: tickets
- name: escalations
OpenClaw 代理绑定(可选)
openclaw:
requireMention: false
agents:
main: general # 单个频道
siren: [general, alerts] # 多个频道
support: # 带选项
channel: tickets
requireMention: true
关键规则
- - guild 是 Discord 服务器 ID(右键服务器 → 复制服务器 ID)
- 频道名称必须小写,不能有空格(Discord 强制要求)
- 线程名称可以有空格和大小写混用
- 代理绑定引用 channels 部分中的频道名称
- requireMention 控制机器人是否需要 @提及才能在该频道中响应
命令
disclaw diff — 显示将要发生的变化
bash
disclaw diff -c disclaw.yaml
disclaw diff -c disclaw.yaml --json # 结构化输出
disclaw diff -c disclaw.yaml --channel alerts # 按频道筛选
显示:受管资源(创建/更新/删除/无操作)、非受管资源、未绑定代理、过期代理、路由健康警告以及置顶消息。
disclaw apply — 应用更改(默认试运行)
bash
disclaw apply -c disclaw.yaml # 试运行(显示将要发生的变化)
disclaw apply -c disclaw.yaml --yes # 实际应用更改
disclaw apply -c disclaw.yaml --prune --yes # 同时删除非受管资源
安全性: 在变更前始终拍摄快照。先创建后删除。绑定和路由网关原子化更新。
disclaw import — 导入非受管的 Discord 资源
bash
disclaw import -c disclaw.yaml # 试运行(显示将要导入的内容)
disclaw import -c disclaw.yaml --yes # 写入配置文件
发现配置中未包含的 Discord 频道/分类/线程并添加它们。同时查找未绑定的 OpenClaw 代理。
disclaw rollback — 从快照恢复
bash
disclaw rollback -c disclaw.yaml # 试运行
disclaw rollback -c disclaw.yaml --yes # 实际回滚
从最近一次应用前的快照恢复 Discord 状态。支持漂移检测(显示自快照以来的变化)。
disclaw validate — 验证配置(无 API 调用)
bash
disclaw validate -c disclaw.yaml
disclaw validate -c disclaw.yaml --json
适用于 CI 环境。检查:模式有效性、空名称、重复频道/线程、绑定引用指向不存在的频道。
筛选标志(diff、apply、import)
bash
--category <名称...> # 按分类名称筛选
--channel <名称...> # 按频道名称筛选
--thread <名称...> # 按线程名称筛选
--agent <名称...> # 按代理名称筛选
--json # 结构化 JSON 输出
网关选项(除 validate 外的所有命令)
bash
--gateway-url <地址> # 覆盖网关 URL(默认:http://127.0.0.1:18789)
--gateway-token <令牌> # 覆盖网关认证令牌
也可通过环境变量设置:OPENCLAWGATEWAYURL、OPENCLAWGATEWAYTOKEN。
常见工作流程
添加新频道
- 1. 编辑 disclaw.yaml — 在相应分类下添加频道
- 验证:disclaw validate -c disclaw.yaml
- 预览:disclaw diff -c disclaw.yaml
- 应用:disclaw apply -c disclaw.yaml --yes
将代理绑定到频道
- 1. 编辑 disclaw.yaml — 在 openclaw.agents 下添加条目
- 预览:disclaw diff -c disclaw.yaml
- 应用:disclaw apply -c disclaw.yaml --yes
这将创建绑定并自动将频道加入路由网关的白名单。
导入现有 Discord 频道
- 1. 运行:disclaw import -c disclaw.yaml(试运行以预览)
- 检查输出
- 运行:disclaw import -c disclaw.yaml --yes(写入配置)
- 验证:disclaw diff -c disclaw.yaml
删除频道
- 1. 从 disclaw.yaml 中移除该频道
- 预览:disclaw apply -c disclaw.yaml --prune
- 应用:disclaw apply -c disclaw.yaml --prune --yes
警告: 删除操作必须使用 --prune。不使用该选项时,被移除的频道会显示为非受管但不会被删除。
重命名频道
- 1. 在 disclaw.yaml 中更改频道名称
- 预览:disclaw diff -c disclaw.yaml(显示删除旧频道 + 创建新频道)
- 应用:disclaw apply -c disclaw.yaml --yes
注意:Discord 不支持通过 API 进行真正的重命名 — disclaw 会创建新频道并(使用 --prune)删除旧频道。
故障排除
未找到 Discord 机器人令牌
- - 确保 openclaw.json 中设置了 channels.discord.token
- 或设置 DISCORDBOTTOKEN 环境变量
网关工具不可用
- - 在 openclaw.json 中添加 gateway.tools.allow: [gateway]
- 否则 disclaw 会自动回退到 CLI 模式
网关认证失败
- - 检查 OPENCLAWGATEWAYTOKEN 环境变量是否与配置中的 gateway.auth.token 匹配
- 或传递 --gateway-token <令牌>
OpenClaw CLI 超时
- - 检查网关是否运行:openclaw status
- 检查网络:curl http://127.0.0.1:18789/
Discord 连接超时
- - 检查互联网连接
- 验证机器人令牌是否有效
- 重试(Discord API 可能不稳定)
应用时权限被拒绝
- - 机器人需要:管理频道、管理线程权限
- 检查机器人在 Discord 服务器设置中的角色位置
安全性
- - 默认试运行 — 所有变更命令都需要 --yes
- 应用前快照 — 自动保存至 .disclaw/snapshots/
- 支持回滚 — disclaw rollback -c disclaw.yaml --yes
- 仅限受管范围 — disclaw 只操作配置中的资源