Discord Context
Problem
OpenClaw is session-based: agents only see messages from conversations where they have an active session. Discord threads that the agent wasn't mentioned in or hasn't interacted with are invisible — there's no built-in tool to read arbitrary channel/thread history.
Additionally, OpenClaw redacts the Discord bot token from openclaw config get and environment variables (by design), so agents cannot make direct Discord API calls using the configured token.
Solution
Store the Discord bot token in a separate file accessible to the agent, then use curl to call the Discord API directly.
Setup (one-time, run as the user)
CODEBLOCK0
Record the path in TOOLS.md so the agent knows where to find it across sessions.
Reading Messages
CODEBLOCK1
Finding Thread/Channel IDs
- - Enable Developer Mode in Discord: User Settings → Advanced → Developer Mode
- Right-click any channel or thread → Copy Channel ID
- Thread IDs and channel IDs work the same way in the API
Key Notes
- - Discord returns messages newest-first by default
- Max
limit is 100 per request; use before/after params to paginate - The bot must be a member of the guild and have View Channel + Read Message History permissions
- Rate limits apply: 50 requests/second per route (respect
429 responses and Retry-After headers)
Response Fields
Each message object contains:
- -
content — message text - INLINECODE9 /
author.global_name — who sent it - INLINECODE11 — when
- INLINECODE12 — message ID (for pagination or reply references)
- INLINECODE13 — the message being replied to (if a reply)
Security Considerations
- - The token file is
chmod 600 and outside the git-tracked workspace - The bot token grants read/write access to all channels the bot is in — treat it like a password
- Prefer read-only API calls; do not use this for sending messages (use OpenClaw's native routing instead)
- If the token is rotated in Discord Developer Portal, update both
openclaw config and the token file
Discord 上下文
问题
OpenClaw 是基于会话的:代理只能看到它们拥有活跃会话的对话中的消息。代理未被提及或未与之交互的 Discord 线程是不可见的——没有内置工具可以读取任意频道/线程历史记录。
此外,OpenClaw 会从 openclaw config get 和环境变量中编辑掉 Discord 机器人令牌(按设计),因此代理无法使用配置的令牌直接调用 Discord API。
解决方案
将 Discord 机器人令牌存储在代理可访问的单独文件中,然后使用 curl 直接调用 Discord API。
设置(一次性操作,以用户身份运行)
bash
将机器人令牌存储在工作区之外的文件中(不会被 git 提交)
echo 你的
DISCORD机器人_令牌 > ~/.openclaw/.discord-bot-token
chmod 600 ~/.openclaw/.discord-bot-token
将路径记录在 TOOLS.md 中,以便代理知道在会话之间在哪里找到它。
读取消息
bash
加载令牌
DISCORD_TOKEN=$(cat ~/.openclaw/.discord-bot-token)
从频道或线程读取最近的消息(线程在 Discord 中也是频道)
curl -s -H Authorization: Bot $DISCORD_TOKEN \
https://discord.com/api/v10/channels/{频道或线程ID}/messages?limit=50 \
| python3 -m json.tool
读取特定消息ID之前的消息(分页)
curl -s -H Authorization: Bot $DISCORD_TOKEN \
https://discord.com/api/v10/channels/{频道ID}/messages?limit=50&before={消息ID} \
| python3 -m json.tool
列出公会频道中的活跃线程
curl -s -H Authorization: Bot $DISCORD_TOKEN \
https://discord.com/api/v10/channels/{父频道ID}/threads/active \
| python3 -m json.tool
查找线程/频道ID
- - 在 Discord 中启用开发者模式:用户设置 → 高级 → 开发者模式
- 右键点击任何频道或线程 → 复制频道ID
- 线程ID和频道ID在API中的工作方式相同
关键说明
- - Discord 默认按最新优先返回消息
- 每次请求的最大 limit 为 100;使用 before/after 参数进行分页
- 机器人必须是公会的成员,并拥有查看频道和读取消息历史权限
- 速率限制适用:每个路由每秒 50 个请求(请尊重 429 响应和 Retry-After 头部)
响应字段
每个消息对象包含:
- - content — 消息文本
- author.username / author.globalname — 发送者
- timestamp — 时间戳
- id — 消息ID(用于分页或回复引用)
- referencedmessage — 被回复的消息(如果是回复)
安全考虑
- - 令牌文件权限为 chmod 600,且位于 git 跟踪的工作区之外
- 机器人令牌授予对机器人所在所有频道的读/写访问权限——请像对待密码一样对待它
- 优先使用只读 API 调用;不要用于发送消息(请使用 OpenClaw 的原生路由)
- 如果在 Discord 开发者门户中轮换令牌,请同时更新 openclaw config 和令牌文件