如何用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两个特权意图。 - 确保机器人在目标论坛频道拥有发送消息的权限。
- 在Discord开发者门户的机器人页面,开启
- 精准过滤帖子:
若只需监控特定论坛频道,可添加频道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
相关产品推荐
相关产品推荐

