Creating a Discord bot that responds to user reactions

Hey everyone! I’m working on a Discord bot for my server and I’m stuck on a specific feature. I want the bot to ask users if they’re sure about doing a paid splash and then react based on their response. Here’s what I’ve got so far:

@bot.command(aliases=['paidwash'])
async def wash(ctx):
    query = await ctx.send('Ready for a paid wash?')
    await query.add_reaction('👍')
    await query.add_reaction('👎')
    await ctx.message.delete()
    
    # This part isn't working
    bot.wait_for('reaction_add', timeout=10.0)
    await ctx.send('Confirmed')

The bot asks the question and adds reactions, but I can’t figure out how to check which reaction the user picked. If they choose :+1:, I want to add them to a queue (haven’t coded that part yet).

Can anyone help me fix this code? I learn best by seeing corrections in action. Thanks!

hey, i had a similar issue. try using a check function with wait_for(). something like:

def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in [‘:+1:’, ‘:-1:’]

reaction, user = await bot.wait_for(‘reaction_add’, timeout=10.0, check=check)

then u can check which emoji they picked. hope this helps!

Having worked on Discord bots before, I can suggest a more robust approach to handle user reactions. Instead of using a timeout, consider implementing an event listener for reactions. This way, your bot can respond whenever a user reacts, not just within a specific timeframe.

Here’s a snippet to illustrate:

@bot.event
async def on_reaction_add(reaction, user):
    if user.bot:
        return
    if str(reaction.emoji) == '👍' and reaction.message.content == 'Ready for a paid wash?':
        await reaction.message.channel.send(f'{user.mention} confirmed for a paid wash. Adding to queue.')
        # Add logic here to include user in the wash queue
    elif str(reaction.emoji) == '👎' and reaction.message.content == 'Ready for a paid wash?':
        await reaction.message.channel.send(f'{user.mention} declined the paid wash.')

This method allows for more flexibility and doesn’t restrict users to a short response window. Remember to handle edge cases and potential errors in your final implementation.

I’ve actually implemented something similar in one of my Discord bots. The key is to use the wait_for() method correctly. Here’s how I’d modify your code:

@bot.command(aliases=['paidwash'])
async def wash(ctx):
    query = await ctx.send('Ready for a paid wash?')
    await query.add_reaction('👍')
    await query.add_reaction('👎')
    await ctx.message.delete()
    
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ['👍', '👎']
    
    try:
        reaction, user = await bot.wait_for('reaction_add', timeout=10.0, check=check)
        if str(reaction.emoji) == '👍':
            await ctx.send('Confirmed! Adding you to the queue.')
            # Add code here to add user to queue
        else:
            await ctx.send('Canceled.')
    except asyncio.TimeoutError:
        await ctx.send('Timed out. Please try again.')

This setup waits for the reaction, checks if it’s from the original user, and responds accordingly. Don’t forget to import asyncio at the top of your script. Hope this helps!