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

使用discord.py能否在论坛线程中添加反应或编辑标签?

Discord机器人线程标签编辑与反应添加解决方案

线程标签编辑说明

Discord线程本身没有独立的标签体系,它的标签是继承自所属父频道的。如果要给线程设置标签,前提是父频道已经创建了对应的标签,之后可以通过修改线程的applied_tags属性来应用这些标签。示例代码如下:

@bot.event
async def on_thread_create(thread):
    # 替换为你要应用的标签ID
    target_tag_id = 123456789012345678
    target_tag = discord.utils.get(thread.parent.tags, id=target_tag_id)
    if target_tag:
        await thread.edit(applied_tags=[target_tag])

给线程添加反应的正确方式

你之前的报错是因为Thread对象没有add_reaction方法——Discord的反应是绑定在消息上的,不是线程本身。下面提供两种可行的实现方式:

方式1:给线程的起始系统消息加反应

线程创建时,父频道会生成一条对应的系统消息,你可以通过fetch_starter_message()获取这条消息,再添加反应:

@bot.event
async def on_thread_create(thread):
    # 先获取线程的起始消息
    await thread.fetch_starter_message()
    if thread.starter_message:
        await thread.starter_message.add_reaction('✅')

方式2:发送自定义消息并根据条件加反应

如果需要根据线程名称是否合规来添加不同反应,可以先在线程内发送一条自定义消息,再针对这条消息添加对应反应,同时保留你原有的名称修正逻辑:

@bot.event
async def on_thread_create(thread):
    # 检查名称是否合规(这里替换成你的判断逻辑)
    is_name_valid = thread.name.startswith('Request #')
    
    # 发送初始提示消息到线程
    reply_msg = await thread.send("线程已初始化完成")
    
    # 根据合规性添加不同反应
    if is_name_valid:
        await reply_msg.add_reaction('✅')
    else:
        await reply_msg.add_reaction('⚠️')
        # 执行名称修正(你的原有逻辑)
        # 这里替换成你的正确编号生成逻辑
        correct_request_number = "001"
        await thread.edit(name=f"Request #{correct_request_number}")

注意事项

  • 使用fetch_starter_message()时,机器人需要拥有读取父频道消息的权限
  • 添加反应需要机器人具备“添加反应”的权限
  • 线程只能应用父频道已存在的标签,无法直接为线程创建新标签

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 00:42:14