Discord.py:wait_for('reaction_add')无法正常工作

时间:2020-06-27 11:24:09

标签: python python-3.x bots discord discord.py

我正在尝试在discord.py上制作一个FAQ机器人,到目前为止进展良好。我想添加一个额外的功能,即当机器人检测到FAQ时,它会直接发送带有两个反应的提示消息-竖起大拇指和按下-并取决于所选的反应,而不是直接发送答案僵尸程序会由用户发送答案或删除提示消息。

现在,当询问FAQ时,机器人会检测到它并发送提示,询问用户是否需要答案,甚至对此做出反应。问题是,一旦机器人完成对Thumbs-down表情符号的反应,提示消息就会被删除。我希望它等待用户做出反应并继续进行操作。

我在做什么错?请帮忙。预先感谢!

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    
    if (message.author.bot):
        return        

    if(isQuestion(message.content)):
        (answer_text, question_text) = answer_question(message.content)

        if answer_text:
            botmessage = await message.channel.send(f"""Do you want the answer to: {question_text} ?""")
            
            await botmessage.add_reaction('\N{THUMBS UP SIGN}')
            await botmessage.add_reaction('\N{THUMBS DOWN SIGN}')

            def checkUp(reaction, user):
                return user == message.author and str(reaction.emoji) == '\N{THUMBS UP SIGN}' or str(reaction.emoji) == '\N{THUMBS DOWN SIGN}'

            try:
                reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=checkUp)
            except asyncio.TimeoutError:
                await botmessage.delete()
            else:
                print(reaction.emoji)
                if reaction.emoji == '\N{THUMBS UP SIGN}':
                    await botmessage.delete()
                    await message.channel.send(answer_text)

                elif reaction.emoji == '\N{THUMBS DOWN SIGN}':
                    await botmessage.delete()

1 个答案:

答案 0 :(得分:2)

问题最可能出在您的“ checkUp”函数上,因为您缺少括号。

user == message.author and str(reaction.emoji) == '\N{THUMBS UP SIGN}' or str(reaction.emoji) == '\N{THUMBS DOWN SIGN}'

我不确定遇到像这样的布尔链时python的默认行为,但可能是因为它把方括号放在了这样的位置:

(user == message.author and str(reaction.emoji) == '\N{THUMBS UP SIGN}') or (str(reaction.emoji) == '\N{THUMBS DOWN SIGN}')

您看到问题了吗?现在,只要某人对消息做出不满意的反应,该支票就会返回True。这意味着它可以对先前的反应做出反应(尽管您之前可能已经执行过此操作,但是我们在这里讨论的是异步)。

修复: 放在这样的括号中:

user == message.author and (str(reaction.emoji) == '\N{THUMBS UP SIGN}' or str(reaction.emoji) == '\N{THUMBS DOWN SIGN}')