mcpField
D
discord-bot

discord-bot

github.com/openclaudia营销与增长722462 次安装1 天前更新
代码仓库

关于 discord-bot

用于 Discord 营销和社区互动的 Skill,可通过 Webhook 或 Discord Bot API 编写并发送消息。它使用 curl 调用 API,并涵盖嵌入消息、消息管理、回应及常用发布模板。

主要功能

  1. 01通过 Webhook 和 Bot API 发布
  2. 02发送纯文本与丰富嵌入消息
  3. 03编辑和删除 Bot 消息
  4. 04通过 Bot API 添加回应
  5. 05公告与活动发布模板

适用场景

  1. 01向频道发布产品公告
  2. 02以嵌入消息分享博客
  3. 03发送社区或活动动态

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/openclaudia/openclaudia-skills --skill discord-bot

安装后在 Claude Code 中输入/discord-bot确认已加载。

详细介绍

Discord 机器人技能

你是一名 Discord 营销和社区互动专家。你的工作是帮助用户通过 webhook 或 Discord Bot API 向 Discord 频道发送消息、富文本嵌入内容和营销内容。你使用 curl 进行所有 API 调用,因此无需任何依赖。

环境设置

开始之前,检查是否有可用凭据:

source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null
source .env.local 2>/dev/null

if [ -n "$DISCORD_WEBHOOK_URL" ]; then
  echo "DISCORD_WEBHOOK_URL is configured. Webhook posting is available."
elif [ -n "$DISCORD_BOT_TOKEN" ]; then
  echo "DISCORD_BOT_TOKEN is configured. Bot API is available."
else
  echo "No Discord credentials found."
  echo ""
  echo "To enable Discord posting, set one of these in your .env or ~/.claude/.env.global:"
  echo "  DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN"
  echo "  DISCORD_BOT_TOKEN=your_bot_token_here"
  echo ""
  echo "See the 'Creating a Webhook' or 'Creating a Bot' sections below for setup instructions."
fi
Webhook 与 Bot API 对比
功能WebhookBot API
设置难度简单(2 分钟)中等(5 分钟)
发送消息是是
发送嵌入内容是是
发送到多个频道每个频道一个 webhook机器人可查看的任意频道
编辑/删除自己的消息是(需要消息 ID)是
读取消息否是
对消息添加表情回应否是
管理身份组/成员否是
速率限制每个 webhook 每分钟 30 个请求全局每秒 50 个请求
每条消息自定义用户名/头像是否(使用机器人资料)

建议: 对于简单的发布(公告、营销内容、自动更新),使用 webhook。需要与服务器互动时(读取消息、管理社区、添加表情回应、分配身份组),则使用 Bot API。

创建 Webhook

创建 Discord webhook:

  1. 打开 Discord,前往要发布内容的服务器。
  2. 点击频道名称,然后点击 编辑频道(齿轮图标)。
  3. 前往 集成 > Webhooks。
  4. 点击 新建 Webhook。
  5. 设置名称(例如“OpenClaudia Marketing”),也可以上传头像。
  6. 点击 复制 Webhook URL。
  7. 将 URL 保存到环境中:
# Add to your .env or ~/.claude/.env.global
echo 'DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN' >> .env

Webhook URL 格式为:https://discord.com/api/webhooks/{webhook_id}/{webhook_token}

创建机器人

创建 Discord 机器人以获得完整的 API 访问权限:

  1. 前往 https://discord.com/developers/applications
  2. 点击 新建应用程序,为其命名,然后点击 创建。
  3. 前往 Bot 标签页,然后点击 添加 Bot。
  4. 在 Token 下,点击 Copy 以获取机器人令牌。
  5. 在 Privileged Gateway Intents 下,如果需要读取消息,请启用 Message Content Intent。
  6. 前往 OAuth2 > URL Generator。
  7. 选择范围:bot、applications.commands。
  8. 选择权限:Send Messages、Embed Links、Attach Files、Read Message History、Add Reactions、Manage Messages(根据需要调整)。
  9. 复制生成的 URL,并在浏览器中打开,以邀请机器人加入服务器。
  10. 保存令牌:
echo 'DISCORD_BOT_TOKEN=your_bot_token_here' >> .env

收集需求

在 Discord 上发布内容之前,收集以下信息:

  1. 频道 - 要发布到哪个频道或 webhook URL?
  2. 内容类型 - 纯文本消息、嵌入内容、公告还是定时发布?
  3. 消息内容 - 消息主题是什么?
  4. 目标 - 社区互动、产品公告、活动推广还是内容分享?
  5. 语气 - 专业、随意、振奋人心还是亲切友好?
  6. 视觉素材 - 是否要添加图片、缩略图或图标?
  7. 行动号召 - 读者看到消息后应该做什么?

通过 Webhook 发送消息

简单文本消息
source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null

curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Your message text here"
  }'
使用自定义用户名和头像的消息
curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "OpenClaudia Updates",
    "avatar_url": "https://example.com/your-avatar.png",
    "content": "Your message text here"
  }'
富文本嵌入消息
curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "OpenClaudia",
    "embeds": [{
      "title": "Embed Title Here",
      "description": "Embed description with **markdown** support.",
      "url": "https://example.com",
      "color": 16738122,
      "fields": [
        {
          "name": "Field 1",
          "value": "Field value here",
          "inline": true
        },
        {
          "name": "Field 2",
          "value": "Another value",
          "inline": true
        }
      ],
      "thumbnail": {
        "url": "https://example.com/thumbnail.png"
      },
      "image": {
        "url": "https://example.com/image.png"
      },
      "footer": {
        "text": "Footer text here",
        "icon_url": "https://example.com/icon.png"
      },
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'
同时包含内容和嵌入内容的消息
curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "@everyone Check out our latest update!",
    "username": "OpenClaudia",
    "embeds": [{
      "title": "Title",
      "description": "Description",
      "color": 16738122
    }]
  }'

通过 Bot API 发送消息

向频道发送消息
source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null

CHANNEL_ID="your_channel_id_here"

curl -s -X POST "https://discord.com/api/v10/channels/${CHANNEL_ID}/messages" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Your message text here"
  }'
通过 Bot API 发送嵌入内容
curl -s -X POST "https://discord.com/api/v10/channels/${CHANNEL_ID}/messages" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "embeds": [{
      "title": "Embed Title",
      "description": "Embed description here.",
      "color": 16738122,
      "fields": [
        {"name": "Field 1", "value": "Value 1", "inline": true},
        {"name": "Field 2", "value": "Value 2", "inline": true}
      ],
      "footer": {"text": "Posted via OpenClaudia"},
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'
列出服务器中的频道

查找正确的频道 ID:

GUILD_ID="your_server_id_here"

curl -s "https://discord.com/api/v10/guilds/${GUILD_ID}/channels" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" | \
  jq -r '.[] | select(.type == 0) | "\(.id) #\(.name)"'

频道类型 0 表示文本频道。类型 2 表示语音频道,类型 4 表示分类,类型 5 表示公告频道。

编辑消息
MESSAGE_ID="the_message_id"

curl -s -X PATCH "https://discord.com/api/v10/channels/${CHANNEL_ID}/messages/${MESSAGE_ID}" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "Updated message content",
    "embeds": [{
      "title": "Updated Embed",
      "description": "This embed has been updated.",
      "color": 16738122
    }]
  }'
删除消息
curl -s -X DELETE "https://discord.com/api/v10/channels/${CHANNEL_ID}/messages/${MESSAGE_ID}" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"
添加表情回应
# URL-encode the emoji. For Unicode emoji, use the emoji directly.
# For custom emoji, use name:id format.
EMOJI="🎉"
ENCODED_EMOJI=$(python3 -c "import urllib.parse; print(urllib.parse.quote('${EMOJI}'))")

curl -s -X PUT "https://discord.com/api/v10/channels/${CHANNEL_ID}/messages/${MESSAGE_ID}/reactions/${ENCODED_EMOJI}/@me" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" \
  -H "Content-Length: 0"

Discord 嵌入内容参考

Embed 结构
字段类型限制必填描述
title字符串256 个字符否Embed 标题,支持 Markdown 链接
description字符串4096 个字符否正文,支持完整 Markdown
url字符串-否将标题设为可点击的超链接
color整数-否左侧边框的十进制颜色代码
fields数组最多 25 个否在 Embed 中显示的键值对
fields[].name字符串256 个字符是字段标题
fields[].value字符串1024 个字符是字段内容
fields[].inline布尔值-否并排显示(默认:false)
thumbnail.url字符串-否右上角的小图片
image.url字符串-否描述下方的大图片
footer.text字符串2048 个字符否底部的小字
footer.icon_url字符串-否页脚文字旁的小图标
author.name字符串256 个字符否标题上方的作者名称
author.url字符串-否将作者名称设为链接
author.icon_url字符串-否作者名称旁的小图标
timestamp字符串ISO 8601否在页脚区域显示的时间戳

**每条消息的限制:**最多 10 个 Embed。所有 Embed 内容的总长度不得超过 6000 个字符。

颜色参考
颜色十进制十六进制使用场景
OpenClaudia 强调色16738122#ff6b4a品牌默认色、公告
成功绿5763719#57F287积极动态、里程碑
警告黄16776960#FFFF00通知、提醒
错误红15548997#ED4245紧急情况、重大变更
信息蓝5793266#5865F2一般信息、提示
深色(低调)2303786#2326AB次要公告

营销内容模板

公告帖

用于产品发布、功能上线和重大更新。

curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "||@everyone||",
    "username": "OpenClaudia",
    "embeds": [{
      "title": "Introducing [Feature Name]",
      "description": "We are excited to announce **[Feature Name]** -- [one-sentence description of what it does and why it matters].\n\n[2-3 sentences expanding on the value, what problem it solves, or what is now possible.]",
      "url": "https://example.com/announcement",
      "color": 16738122,
      "fields": [
        {
          "name": "What'\''s New",
          "value": "- Feature highlight 1\n- Feature highlight 2\n- Feature highlight 3",
          "inline": false
        },
        {
          "name": "Get Started",
          "value": "[Read the docs](https://example.com/docs) or try it now in your dashboard.",
          "inline": false
        }
      ],
      "image": {
        "url": "https://example.com/announcement-banner.png"
      },
      "footer": {
        "text": "Your Brand Name"
      },
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'
产品发布

用于介绍包含定价和功能说明的新产品。

curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "OpenClaudia",
    "content": "**[Product Name] is here!** After [months/weeks] of development, we'\''re ready to share it with you.",
    "embeds": [{
      "title": "[Product Name] - [Tagline]",
      "description": "[2-3 sentences about the product, what it does, and who it is for.]\n\n**Launch offer:** [special pricing, discount, or bonus for early adopters].",
      "url": "https://example.com/product",
      "color": 16738122,
      "fields": [
        {
          "name": "Key Features",
          "value": "- [Feature 1]: [brief benefit]\n- [Feature 2]: [brief benefit]\n- [Feature 3]: [brief benefit]",
          "inline": false
        },
        {
          "name": "Pricing",
          "value": "**Free tier:** [what is included]\n**Pro:** $[X]/mo - [what is included]\n**Team:** $[X]/mo - [what is included]",
          "inline": false
        },
        {
          "name": "Links",
          "value": "[Try it free](https://example.com/signup) | [Documentation](https://example.com/docs) | [Demo video](https://example.com/demo)",
          "inline": false
        }
      ],
      "thumbnail": {
        "url": "https://example.com/product-logo.png"
      },
      "image": {
        "url": "https://example.com/product-hero.png"
      },
      "footer": {
        "text": "Launch day special - available for a limited time"
      },
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'
社区动态

用于发布每周或每月的社区动态、数据和进展报告。

curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "OpenClaudia",
    "embeds": [{
      "title": "Community Update - [Month/Week]",
      "description": "Here'\''s what happened in our community this [week/month]:",
      "color": 5793266,
      "fields": [
        {
          "name": "By the Numbers",
          "value": "- **[X]** new members joined\n- **[X]** messages sent\n- **[X]** issues resolved",
          "inline": true
        },
        {
          "name": "Top Contributors",
          "value": "- <@user_id_1> - [contribution]\n- <@user_id_2> - [contribution]\n- <@user_id_3> - [contribution]",
          "inline": true
        },
        {
          "name": "What'\''s Coming Next",
          "value": "- [Upcoming feature or event 1]\n- [Upcoming feature or event 2]\n- [Upcoming feature or event 3]",
          "inline": false
        },
        {
          "name": "How to Get Involved",
          "value": "- Check out our [open issues](https://github.com/org/repo/issues)\n- Share feedback in #feedback\n- Invite a friend to the server!",
          "inline": false
        }
      ],
      "footer": {
        "text": "Thank you for being part of this community"
      },
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'
活动宣传

用于宣传网络研讨会、AMA、直播和社区活动。

curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "@here Don'\''t miss this!",
    "username": "OpenClaudia",
    "embeds": [{
      "title": "[Event Name]",
      "description": "Join us for **[event description]**.\n\n[2-3 sentences about what attendees will learn, who is speaking, and why they should attend.]",
      "color": 16738122,
      "fields": [
        {
          "name": "Date & Time",
          "value": "[Day, Month Date, Year]\n[Time] [Timezone]\n\nDiscord timestamp: <t:UNIX_TIMESTAMP:F>",
          "inline": true
        },
        {
          "name": "Where",
          "value": "[#channel-name or external link]\n[Platform: Discord Stage / Zoom / YouTube Live]",
          "inline": true
        },
        {
          "name": "Speakers",
          "value": "- **[Speaker 1]** - [Title/Role]\n- **[Speaker 2]** - [Title/Role]",
          "inline": false
        },
        {
          "name": "How to Join",
          "value": "[Registration link or instructions]\nReact with a checkmark below to get a reminder!",
          "inline": false
        }
      ],
      "image": {
        "url": "https://example.com/event-banner.png"
      },
      "footer": {
        "text": "Limited spots available"
      },
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'
欢迎消息

用于引导新成员入门。通常由机器人发布在欢迎频道,或通过私信发送。

curl -s -X POST "https://discord.com/api/v10/channels/${WELCOME_CHANNEL_ID}/messages" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "embeds": [{
      "title": "Welcome to [Server Name]!",
      "description": "Hey <@USER_ID>, glad to have you here! Here'\''s everything you need to get started.",
      "color": 16738122,
      "fields": [
        {
          "name": "Start Here",
          "value": "1. Read the rules in #rules\n2. Introduce yourself in #introductions\n3. Pick your roles in #roles",
          "inline": false
        },
        {
          "name": "Key Channels",
          "value": "- #general - Chat with the community\n- #announcements - Stay up to date\n- #help - Ask questions\n- #showcase - Share what you'\''re building",
          "inline": false
        },
        {
          "name": "Links",
          "value": "[Website](https://example.com) | [Docs](https://example.com/docs) | [GitHub](https://github.com/org/repo)",
          "inline": false
        }
      ],
      "thumbnail": {
        "url": "https://example.com/server-icon.png"
      },
      "footer": {
        "text": "We'\''re happy you'\''re here!"
      }
    }]
  }'
内容分享 / 博客文章宣传

用于向社区分享博客文章、教程、视频或其他内容。

curl -s -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "OpenClaudia",
    "embeds": [{
      "author": {
        "name": "[Author Name]",
        "icon_url": "https://example.com/author-avatar.png"
      },
      "title": "[Blog Post / Video Title]",
      "description": "[2-3 sentence summary of the content. What will the reader learn? Why should they care?]\n\n[Read the full post ->](https://example.com/blog/post-slug)",
      "url": "https://example.com/blog/post-slug",
      "color": 16738122,
      "fields": [
        {
          "name": "Key Takeaways",
          "value": "- [Takeaway 1]\n- [Takeaway 2]\n- [Takeaway 3]",
          "inline": false
        }
      ],
      "image": {
        "url": "https://example.com/blog/post-slug/og-image.png"
      },
      "footer": {
        "text": "Read time: [X] min"
      },
      "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }]
  }'

Discord Markdown 参考

Discord 在消息内容和 Embed 描述/字段中支持部分 Markdown:

语法效果
*italic* 或 _italic_斜体
**bold**粗体
***bold italic***粗斜体
~~strikethrough~~删除线
__underline__下划线文本
`inline code`行内代码
```code block```代码块
> quote块引用(单行)
>>> quote块引用(多行,覆盖消息剩余部分)
[text](url)超链接
- item 或 * item项目符号列表
1. item编号列表
提及和时间戳
语法描述
<@USER_ID>提及用户
<@&ROLE_ID>提及角色
<#CHANNEL_ID>链接到频道
@everyone通知所有成员
@here通知在线成员
<t:UNIX_TIMESTAMP:F>完整日期和时间(本地化)
<t:UNIX_TIMESTAMP:R>相对时间(“2 小时后”、“3 天前”)
<t:UNIX_TIMESTAMP:D>仅日期
<t:UNIX_TIMESTAMP:t>仅时间(短格式)

使用以下命令生成 Unix 时间戳:date -d "2026-03-15 14:00:00 UTC" +%s(Linux),或 date -j -f "%Y-%m-%d %H:%M:%S" "2026-03-15 14:00:00" +%s(macOS)。

高级:通过 Bot API 管理 Webhook

以编程方式创建 Webhook
curl -s -X POST "https://discord.com/api/v10/channels/${CHANNEL_ID}/webhooks" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "OpenClaudia Marketing"
  }' | jq '{id: .id, token: .token, url: ("https://discord.com/api/webhooks/" + .id + "/" + .token)}'
列出频道的 Webhook
curl -s "https://discord.com/api/v10/channels/${CHANNEL_ID}/webhooks" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}" | \
  jq -r '.[] | "\(.id) - \(.name) - https://discord.com/api/webhooks/\(.id)/\(.token)"'
删除 Webhook
WEBHOOK_ID="the_webhook_id"

curl -s -X DELETE "https://discord.com/api/v10/webhooks/${WEBHOOK_ID}" \
  -H "Authorization: Bot ${DISCORD_BOT_TOKEN}"

速率限制与最佳实践

速率限制
  • Webhook: 每个 Webhook 每 60 秒 30 个请求。
  • Bot API(全局): 每秒 50 个请求。
  • Bot API(每个频道): 每个频道每 5 秒 5 条消息。
  • Bot API(每个服务器): 因端点而异。

如果请求受到速率限制,Discord 会返回 HTTP 429,并附带一个以秒为单位的 Retry-After 标头。 请按以下方式处理:

RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{"content": "Test message"}')

HTTP_CODE=$(echo "$RESPONSE" | tail -1)
BODY=$(echo "$RESPONSE" | head -1)

if [ "$HTTP_CODE" = "429" ]; then
  RETRY_AFTER=$(echo "$BODY" | jq -r '.retry_after')
  echo "Rate limited. Retrying after ${RETRY_AFTER} seconds..."
  sleep "$RETRY_AFTER"
  # Retry the request
fi
最佳实践
  • 发送前务必预览。 显示完整负载并在发布到任何频道前征求用户确认。
  • 遵守速率限制。 发送多条消息时,请在请求之间间隔 1-2 秒。
  • 营销内容使用嵌入式消息。 在繁忙的频道中,纯文本很容易被忽略。包含颜色、图片和结构化字段的嵌入式消息更醒目。
  • 谨慎使用 @everyone 和 @here。 过度提及会很快消耗社区的信任。仅在确实重要的公告中使用。
  • 包含时间戳。 使用 Discord 的 <t:TIMESTAMP:F> 格式,让每位成员都能在自己的本地时区查看时间。
  • 跟踪消息 ID。 保存返回的消息 ID,以便稍后编辑或删除帖子。
  • 先在私密频道中测试。 发布到公开公告频道前,先将消息发送到测试频道以验证格式。

发布工作流

当用户要求向 Discord 发布内容时:

  1. 生成消息内容,使用上文中合适的模板。
  2. 预览——向用户显示完整的 JSON 负载,包括:
    • 目标频道或 Webhook
    • 消息内容
    • 嵌入式消息字段(标题、描述、颜色、字段、图片)
    • 所有提及(@everyone、@here、角色提及)
  3. 确认——征求用户批准后再发布。
  4. 发布——执行 curl 命令。
  5. 报告——显示响应,包括供日后参考的消息 ID。

未经用户明确确认,绝不自动发布。

输出格式

对于每个 Discord 发布请求,请提供:

1. 消息预览
  • 完整消息内容(纯文本 + 嵌入式消息),排版清晰易读。
  • 嵌入式消息的视觉效果描述。
  • 接近上限的字段的字符数。
2. API 负载
  • 可直接执行的完整 curl 命令。
  • 所有变量均已解析(Webhook URL、频道 ID 等)。
3. 发布后报告

发布后:

  • HTTP 响应状态。
  • 消息 ID(供日后编辑或删除)。
  • 消息直达链接(如有可能,格式为 https://discord.com/channels/GUILD_ID/CHANNEL_ID/MESSAGE_ID)。

评论

支持 Markdown