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

Python开发Discord Bot时user.bot未正确识别机器人账号问题

Discord Bot user.bot判断失效问题修复方案

核心问题原因

你的逻辑分支存在设计错误:现有代码仅将await client.wait_until_ready()包裹在if not user.bot判断内,后续发消息、移除反应的业务逻辑没有纳入判断范围,无论触发反应的用户是不是bot,都会执行移除反应的操作,这是bot自身的反应也被删除的根本原因。

其他存在的语法问题

  • 事件回调函数声明缺少def关键字,正确语法为async def on_reaction_add(reaction, user):
  • 代码缩进不规范,嵌套try块的缩进层级错误
  • 重复声明了两个完全相同的异常捕获分支,属于无效代码

修复后可运行代码

@client.event
async def on_reaction_add(reaction, user):
    # 统一等待客户端初始化完成
    await client.wait_until_ready()
    # 仅非bot用户触发的反应执行业务逻辑
    if not user.bot:
        try:
            channel = client.get_channel(welcome_channel_id)
            await channel.send(message)
            await reaction.remove(user)
        except Exception as e:
            raise e

可选优化建议

如果需要监听缓存外历史消息的反应事件,可替换为on_raw_reaction_add事件,避免旧消息的反应触发逻辑失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 05:54:01