You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用discord.py实现Discord Bot实时回复论坛新帖子?

实现Discord论坛新帖子监控与自动回复(discord.py)

核心实现逻辑

discord.py v2.x版本提供了on_thread_create事件,可直接监听论坛帖子的创建操作。当新帖子生成时,通过该事件回调发送指定提示消息即可。

完整代码片段

import discord
from discord.ext import commands

# 初始化机器人并配置必要意图
intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print(f"已登录为 {bot.user} (ID: {bot.user.id})")
    print("------")

# 监听论坛帖子创建事件
@bot.event
async def on_thread_create(thread):
    # 仅处理论坛频道下的帖子(排除普通服务器线程)
    if isinstance(thread.parent, discord.ForumChannel):
        # 构造并发送回复消息
        reply_content = f"Hello {thread.owner.mention}, please stay in this thread and do not make duplicates. Thanks!"
        await thread.send(reply_content)

# 替换为你的机器人Token
bot.run("YOUR_BOT_TOKEN")

关键注意事项

  • 权限与意图配置:
    • 在Discord开发者门户的机器人页面,开启Message Content Intent和Server Members Intent两个特权意图。
    • 确保机器人在目标论坛频道拥有发送消息的权限。
  • 精准过滤帖子:
    若只需监控特定论坛频道,可添加频道ID判断:
    # 示例:仅监控指定ID的论坛频道
    TARGET_FORUM_IDS = [1234567890, 0987654321]
    if thread.parent.id in TARGET_FORUM_IDS:
        await thread.send(reply_content)
    
  • 版本要求:必须使用discord.py v2.x及以上版本,旧版本不支持论坛频道的相关事件。

内容的提问来源于stack exchange,提问作者Shockshwat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:07:11