0

I want to make a rock paper scissors command on discord, but my bot does not respond to a person's reactions.

The rpsai variable is for my rock paper scissors 'AI' which determines rock, paper or scissors randomly.

from random import randint
rpsai = randint(1,3)
async def on_reaction_add(reaction, user):
    if reaction == "":
        if rpsai==1: 
           await message.channel.send('I pick Rock, we tie!')
        if rpsai==2:
           await message.channel.send('I pick Paper, I win!')
        if rpsai==3:
           await message.chanel.send('I pick Scissors, I lose')
Bagle
  • 2,326
  • 3
  • 12
  • 36
  • Do you have all your imports? Is you client/ bot defined? Are you using commands or a message event? Please include this in your question, because this question feels incomplete. – Bagle Mar 27 '21 at 03:24
  • there is so much added code to this with different features and i didn't want to include the entirety of it, a lot of it is a mess, I have discord and random imported. – Minecow 12345 Mar 27 '21 at 03:53
  • Is this code under a command, like `@client.command` or `@bot.command`? – Bagle Mar 27 '21 at 08:39
  • probably woulda been easier to set it up under commands, but it is just waiting until it hears !rps – Minecow 12345 Mar 28 '21 at 00:03
  • Where is your bot waiting for !rps then? `on_reaction_add` is an event (`client.event`, `bot.event`, etc.). The code you provided would work as in: `Receive reaction from ANY message` -> `If reaction is ` -> `Send message`. You would want to put this into a command and use [a check](https://stackoverflow.com/questions/65082883/discord-py-detecting-reactions) – Bagle Mar 29 '21 at 00:55

1 Answers1

0

Here are some issues with the code you provided:

  1. on_reaction_add is an event function. This is not recommended for what you're trying to do, and it would be preferable if you used the .command() instead.
  2. You need to use wait_for() if you're using a command. Your bot isn't going to be listening for a reaction until you tell it to via wait_for().

Here is some code that may help fix these problems:

@client.command() # or @bot.command() depending on what you're using
async def rps(ctx): # you mentioned 'AI', so you won't need any arguments
    msg = await ctx.send("✂...")

    # adding reactions
    await msg.add_reaction("")
    await msg.add_reaction("")
    await msg.add_reaction("✂")

    bot_choice = random.randint(1, 3) # picking a random number from 1 to 3
    
    # this is the check and wait_for
    def check(reaction, user):
        return user == ctx.author and str(reaction) in ["", "", "✂"] and reaction.message == msg

    reaction, user = await client.wait_for("reaction_add", check=check) # or bot.wait_for
    
    if str(reaction.emoji) == "":
        if bot_choice == 1: # Rock
            await ctx.send('I pick Rock, we tie!')
        if bot_choice == 2: # Paper
            await ctx.send('I pick Paper, I win!')
        if bot_choice == 3: # Scissors
           await ctx.send('I pick Scissors, I lose')
    # Then you do the same thing for the others. Don't forget to add this when including Paper and Scissors!

Here is this exact code working below. I renamed it test since I already have a rps command, but you get the point.

Working Code rps

Other References:

Bagle
  • 2,326
  • 3
  • 12
  • 36