Skip to content

💬 项目案例|Build a Discord Agent Bot:社区运营助手怎么做

📌 Discord Bot 是 Telegram Bot 的升级版——更丰富的交互(Slash Command、Embed、Reaction)、更大的用户群(游戏/技术社区)。如果你想让 Agent 服务一个社群,Discord 是首选。


01|适合谁 & MVP

适合:有 Telegram Bot 基础,想服务更大的社群用户

MVP:一个 Discord Bot,支持 /ask 命令查询知识库,支持 @Bot 触发对话


02|技术架构

Discord Server → Discord Gateway → discord.py Bot → Agent
                 Slash Commands    Message Events    LLM + Tools

关键差异 vs Telegram:Discord 有 Slash Command(/ask)、Embed 消息格式、权限系统(Bot 需要特定权限才能读消息/发消息/加 Reaction)


03|核心代码

python
import discord
from discord import app_commands

bot = discord.Client(intents=discord.Intents.default())
tree = app_commands.CommandTree(bot)

@tree.command(name="ask", description="向 AI 助手提问")
async def ask(interaction: discord.Interaction, question: str):
    await interaction.response.defer()  # 告诉 Discord "处理中"
    answer = agent.chat(str(interaction.user.id), question)
    await interaction.followup.send(answer[:2000])  # Discord 消息 2000 字限制

@bot.event
async def on_message(message):
    if message.author == bot.user: return
    if bot.user.mentioned_in(message):
        answer = agent.chat(str(message.author.id), message.content)
        await message.reply(answer[:2000])

04|生产化补丁

  • Discord 单条消息 2000 字限制 → 超长回答分多条发送或提供摘要 + "回复'详情'查看完整版"
  • Slash Command 3 秒超时 → 用 defer() 先占位再异步返回
  • 多 Server 部署 → 每个 Server 独立的知识库/配置

05|常见坑

修复
Bot 读取不到消息检查 Intents(需要 Message Content Intent)
Slash Command 超时defer() + followup.send()
多用户并发每个用户独立的 Agent session

🍋 本文为 AI Agent 学习路线 · 项目案例库。© 2026 AI小柠檬。

© 2026 🍋AI小柠檬 · 内容原创,转载请注明出处