2
votes

I'm trying to make an FAQ bot on discord.py and it was going good so far. I wanted to add an extra feature where when a FAQ is detected by the bot, instead of directly sending the answer, the bot sends a prompt message with two reactions -- thumbs-up and thumbs-down -- and depending on the reaction selected by the user, the bot either sends the answer or deletes the prompt message.

Now when an FAQ is asked, the bot detects it and sends the prompt asking if the user wants the answer and even reacts to it. The problem is, the prompt message gets deleted as soon as the bot finishes reacting with the Thumbs-down emoji. I want it to wait for the user to react and proceed accordingly.

What am I doing wrong? Please help. Thanks in advance!

@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

1 Answers

2
votes

The problem most likely lies in your "checkUp" function, as you are missing parentheses.

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

I am not exactly sure about the default behaviour of python when encountering a boolean chain like this, but it might be that it puts the brackets as such:

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

Do you see the problem? Now as long as someone reacts with thumbs down to the message, the check returns True. This means it could react to its previous own reaction (although you might have executed this before, we are talking about async here).

Fix: Put parentheses as such:

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