user13524876
user13524876

Reputation:

Discord.py have a user choose between 2 reactions

I'm trying to make my discord bot have a higher or lower game where the player clicks an up or down arrow to guess, but I'm struggling to work out how check if the reaction is the first one or the second one.

Here is my code so far (its in a cog):

  @commands.command()
  async def highlow(self, ctx, limit:int = 100, *args):
    num = random.randint(0, limit)

    msg = await ctx.reply("The number is {num} \nWHould you like to go (h)igher or (l)ower")

    await self.client.add_reaction(msg, emoji="<:arrow_up:>")
    await self.client.add_reaction(msg, emoji="<:arrow_down:>")

    def check(reaction, user):

      return reaction.message == msg and str(reaction.emoji) ==  '<:arrow_down:>'

    

    await self.client.wait_for('reaction_add', check=check)

(Also, I am aware that self.client.add_reaction will not work, its some old code I just quickly copied over to test out)

How do I make it so that I can check either reaction instead of just one?

Upvotes: 0

Views: 391

Answers (1)

Dominik
Dominik

Reputation: 3592

To add a reaction we use msg.add_reaction("Your reaction") in your case.

If you want to check the reaction of a user we wait_for a reaction_add.

You can have a look at the following code:

try:
    reaction, user = await client.wait_for('reaction_add')
    while user == client.user:
        reaction, user = await client.wait_for('reaction_add')
    if str(reaction.emoji) == "YourEmoji": # First reaction
        # Do what you want to do
    if str(reaction.emoji) == "YourEmoji": # Second reaction
        # Do what you want to do
except Exception: # Timeout can be added for example
    return

Here we check that the bot reaction is not counting as a valid reaction but instead a user one. You can then add more if-statements in your code.

Upvotes: 1

Related Questions