discord

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Discord Bot API

Discord Bot API

Use the Discord Bot API via direct
curl
calls to manage channels, guilds, messages, and users.
Official docs:
https://discord.com/developers/docs

通过直接调用
curl
来使用Discord Bot API,以管理频道、服务器、消息和用户
官方文档:
https://discord.com/developers/docs

When to Use

适用场景

Use this skill when you need to:
  • Send messages to specific channels
  • Read messages from channels
  • Manage channels (create, edit, delete)
  • Get server info (guilds, members, roles)
  • React to messages and moderate content
  • Create webhooks programmatically
For simple message posting, use
discord-webhook
skill instead.

当你需要以下操作时,可以使用此技能:
  • 给指定频道发送消息
  • 从频道读取消息
  • 管理频道(创建、编辑、删除)
  • 获取服务器信息(服务器、成员、角色)
  • 对消息添加反应并管理内容
  • 以编程方式创建Webhook
如果只是简单发送消息,建议使用
discord-webhook
技能。

Prerequisites

前置条件

1. Create Application

1. 创建应用

  1. Go to Discord Developer Portal
  2. Click "New Application" and give it a name
  3. Go to "Bot" section and click "Add Bot"
  1. 访问Discord开发者平台
  2. 点击「新建应用」并为其命名
  3. 进入「机器人」板块,点击「添加机器人」

2. Get Bot Token

2. 获取机器人令牌

  1. In Bot section, click "Reset Token"
  2. Copy the token (shown only once)
bash
export DISCORD_BOT_TOKEN="MTIzNDU2Nzg5MDEyMzQ1Njc4OQ.ABcDeF.xxxxx..."
  1. 在机器人板块中,点击「重置令牌」
  2. 复制令牌(仅显示一次)
bash
export DISCORD_BOT_TOKEN="MTIzNDU2Nzg5MDEyMzQ1Njc4OQ.ABcDeF.xxxxx..."

3. Enable Intents (if needed)

3. 启用意图(如有需要)

In Bot section, enable:
  • Presence Intent (for user status)
  • Server Members Intent (for member list)
  • Message Content Intent (for reading messages)
在机器人板块中,启用:
  • Presence Intent(用于获取用户在线状态)
  • Server Members Intent(用于获取成员列表)
  • Message Content Intent(用于读取消息内容)

4. Invite Bot to Server

4. 将机器人邀请至服务器

  1. Go to OAuth2 → URL Generator
  2. Select scopes:
    bot
    ,
    applications.commands
  3. Select permissions needed (e.g., Send Messages, Read Messages)
  4. Copy URL and open in browser to invite
  1. 进入OAuth2 → URL生成器
  2. 选择权限范围:
    bot
    applications.commands
  3. 选择所需权限(例如:发送消息、读取消息)
  4. 复制链接并在浏览器中打开,完成机器人邀请

5. Get IDs

5. 获取ID

Enable Developer Mode: User Settings → Advanced → Developer Mode Right-click any channel/user/server → Copy ID

Important: When using
$VAR
in a command that pipes to another command, wrap the command containing
$VAR
in
bash -c '...'
. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"' | jq .
开启开发者模式:用户设置 → 高级 → 开发者模式 右键点击任意频道/用户/服务器 → 复制ID

重要提示: 当在包含管道的命令中使用
$VAR
时,请将包含
$VAR
的命令用
bash -c '...'
包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"' | jq .

How to Use

使用方法

Base URL:
https://discord.com/api/v10
Authorization header:
Authorization: Bot YOUR_TOKEN

基础URL:
https://discord.com/api/v10
授权头:
Authorization: Bot 你的令牌

1. Get Current Bot User

1. 获取当前机器人用户

bash
bash -c 'curl -s "https://discord.com/api/v10/users/@me" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, username, discriminator}'

bash
bash -c 'curl -s "https://discord.com/api/v10/users/@me" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, username, discriminator}'

2. Send Message to Channel

2. 给频道发送消息

Write to
/tmp/discord_request.json
:
json
{
  "content": "Hello from bot!"
}
Then run (replace
<your-channel-id>
with the actual channel ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/channels/<your-channel-id>/messages" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json'

写入内容到
/tmp/discord_request.json
json
{
  "content": "来自机器人的问候!"
}
然后执行命令(将
<your-channel-id>
替换为实际的频道ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/channels/<your-channel-id>/messages" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json'

3. Send Embed Message

3. 发送嵌入消息

Write to
/tmp/discord_request.json
:
json
{
  "embeds": [
    {
      "title": "Bot Message",
      "description": "This is from the bot API",
      "color": 5793266
    }
  ]
}
Then run (replace
<your-channel-id>
with the actual channel ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/channels/<your-channel-id>/messages" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json'

写入内容到
/tmp/discord_request.json
json
{
  "embeds": [
    {
      "title": "机器人消息",
      "description": "这是通过Bot API发送的消息",
      "color": 5793266
    }
  ]
}
然后执行命令(将
<your-channel-id>
替换为实际的频道ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/channels/<your-channel-id>/messages" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json'

4. Get Channel Info

4. 获取频道信息

Replace
<your-channel-id>
with the actual channel ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, name, type, guild_id}'

<your-channel-id>
替换为实际的频道ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, name, type, guild_id}'

5. Get Channel Messages

5. 获取频道消息

Replace
<your-channel-id>
with the actual channel ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>/messages?limit=10" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, author: .author.username, content}'

<your-channel-id>
替换为实际的频道ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>/messages?limit=10" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, author: .author.username, content}'

6. Get Specific Message

6. 获取指定消息

Replace
<your-channel-id>
and
<your-message-id>
with the actual IDs:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>/messages/<your-message-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, content, author: .author.username}'

<your-channel-id>
<your-message-id>
替换为实际的ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>/messages/<your-message-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, content, author: .author.username}'

7. Delete Message

7. 删除消息

Replace
<your-channel-id>
and
<your-message-id>
with the actual IDs:
bash
bash -c 'curl -s -X DELETE "https://discord.com/api/v10/channels/<your-channel-id>/messages/<your-message-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"'

<your-channel-id>
<your-message-id>
替换为实际的ID:
bash
bash -c 'curl -s -X DELETE "https://discord.com/api/v10/channels/<your-channel-id>/messages/<your-message-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"'

8. Add Reaction

8. 添加消息反应

Replace
<your-channel-id>
and
<your-message-id>
with the actual IDs:
bash
bash -c 'curl -s -X PUT "https://discord.com/api/v10/channels/<your-channel-id>/messages/<your-message-id>/reactions/%F0%9F%91%8D/@me" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Length: 0"'
Note: Emoji must be URL encoded (👍 =
%F0%9F%91%8D
)

<your-channel-id>
<your-message-id>
替换为实际的ID:
bash
bash -c 'curl -s -X PUT "https://discord.com/api/v10/channels/<your-channel-id>/messages/<your-message-id>/reactions/%F0%9F%91%8D/@me" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Length: 0"'
注意:表情符号必须进行URL编码(👍 =
%F0%9F%91%8D

9. Get Guild (Server) Info

9. 获取服务器信息

Replace
<your-guild-id>
with the actual guild ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, name, member_count, owner_id}'

<your-guild-id>
替换为实际的服务器ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '{id, name, member_count, owner_id}'

10. List Guild Channels

10. 列出服务器频道

Replace
<your-guild-id>
with the actual guild ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>/channels" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, name, type}'

<your-guild-id>
替换为实际的服务器ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>/channels" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, name, type}'

11. Get Guild Members

11. 获取服务器成员

Replace
<your-guild-id>
with the actual guild ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>/members?limit=10" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {user: .user.username, nick, joined_at}'

<your-guild-id>
替换为实际的服务器ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>/members?limit=10" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {user: .user.username, nick, joined_at}'

12. Get Guild Roles

12. 获取服务器角色

Replace
<your-guild-id>
with the actual guild ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>/roles" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, name, color, position}'

<your-guild-id>
替换为实际的服务器ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/guilds/<your-guild-id>/roles" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, name, color, position}'

13. Create Webhook

13. 创建Webhook

Write to
/tmp/discord_request.json
:
json
{
  "name": "My Webhook"
}
Then run (replace
<your-channel-id>
with the actual channel ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/channels/<your-channel-id>/webhooks" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json' | jq '{id, token, url: "https://discord.com/api/webhooks/\(.id)/\(.token)"}'

写入内容到
/tmp/discord_request.json
json
{
  "name": "我的Webhook"
}
然后执行命令(将
<your-channel-id>
替换为实际的频道ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/channels/<your-channel-id>/webhooks" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json' | jq '{id, token, url: "https://discord.com/api/webhooks/\(.id)/\(.token)"}'

14. List Channel Webhooks

14. 列出频道Webhook

Replace
<your-channel-id>
with the actual channel ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>/webhooks" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, name, token}'

<your-channel-id>
替换为实际的频道ID:
bash
bash -c 'curl -s "https://discord.com/api/v10/channels/<your-channel-id>/webhooks" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"' | jq '.[] | {id, name, token}'

15. Create Text Channel

15. 创建文本频道

Write to
/tmp/discord_request.json
:
json
{
  "name": "new-channel",
  "type": 0
}
Then run (replace
<your-guild-id>
with the actual guild ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/guilds/<your-guild-id>/channels" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json' | jq '{id, name}'

写入内容到
/tmp/discord_request.json
json
{
  "name": "new-channel",
  "type": 0
}
然后执行命令(将
<your-guild-id>
替换为实际的服务器ID):
bash
bash -c 'curl -s -X POST "https://discord.com/api/v10/guilds/<your-guild-id>/channels" -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" -H "Content-Type: application/json" -d @/tmp/discord_request.json' | jq '{id, name}'

Channel Types

频道类型

TypeDescription
0Text channel
2Voice channel
4Category
5Announcement
13Stage
15Forum

类型描述
0文本频道
2语音频道
4分类频道
5公告频道
13舞台频道
15论坛频道

Guidelines

注意事项

  1. Rate limits: Check
    X-RateLimit-*
    headers; implement backoff
  2. Token security: Never expose bot tokens
  3. Permissions: Bot needs appropriate permissions for each action
  4. Intents: Enable required intents in Developer Portal
  5. API version: Use
    /v10
    for latest stable API
  1. 速率限制:检查
    X-RateLimit-*
    响应头;实现退避机制
  2. 令牌安全:切勿泄露机器人令牌
  3. 权限设置:机器人需要具备对应操作的权限
  4. 意图配置:在开发者平台启用所需的意图
  5. API版本:使用
    /v10
    作为最新稳定版API