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

Discord.py反应检测功能失效,await wait_for执行后无响应

问题分析与修复

你的代码核心问题是**checkReaction函数没有返回判断结果**。bot.wait_for的check参数需要一个能返回布尔值的函数,只有当函数返回True时,才会结束等待并执行后续代码。你的函数里只写了判断表达式,但没有用return返回它,导致函数默认返回None,永远无法通过检测,wait_for会一直阻塞,后续代码自然不会运行。

修复后的代码

修正checkReaction函数:

def checkReaction(reaction, user):
    return user != bot.user and (str(reaction.emoji) == ':x:' or str(reaction.emoji) == '✂' or str(reaction.emoji) == ':snake:')

调用部分保持不变:

reaction, user = await bot.wait_for("reaction_add", check=checkReaction)

if str(reaction.emoji) == ':x:':
    #Call Tic Tac Toe
    print('test')
    await ttt(ctx, bot)

额外优化建议

  • 对于Discord默认表情,直接用表情字符匹配更可靠,避免因表情名称字符串不匹配导致的问题:
    def checkReaction(reaction, user):
        return user != bot.user and str(reaction.emoji) in ['❌', '✂', '🐍']
    
  • 如果需要限定只检测特定消息的反应,可以在check函数里增加消息判断,比如:
    def checkReaction(reaction, user):
        return user != bot.user and str(reaction.emoji) in ['❌', '✂', '🐍'] and reaction.message.id == target_message_id
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 20:21:00