Reputation: 414
I am writing a feature in my bot which allows 2 users to duel each other, and the way it works is that the bot sends a message telling everyone to get ready and then after a random number of seconds pass the bot reactions with a certain emoji on the message and the first person in the duel reacts to the message wins. It a simple I idea but I can't figure out how to see if a certain user has reacted to the message. My question is - is this possible or do I have to figure out another approach to this?
Upvotes: 0
Views: 4451
Reputation: 33
This is definitely possible with wait_for
which is documented here.
The wait_for
function waits for any specified event listed in the event reference.
It takes in three parameters Event
, Check (optional)
and timeout(optional)
there are examples in the documentation but for you it would be something like:
bot = commands.Bot(command_prefix='.', case_insensitive=True)
@bot.command()
async def game(ctx):
# Whatever you want here
msg = await ctx.send("React first to win")
await msg.add_reaction('👍')
def check(reaction, user):
return str(reaction.emoji) == '👍' and user != bot.user
try:
# Timeout parameter is optional but sometimes can be useful
reaction, user = await bot.wait_for('reaction_add', timeout=30, check=check)
# Will wait until a user reacts with the specified checks then continue on with the code
await ctx.send(f"Congratulations {user.name} you won!")
except asyncio.TimeoutError:
# when wait_for reaches specified timeout duration (in this example it is 30 seconds)
await ctx.send("You ran out of time!")
The Timeout=30
parameter is 100% optional you can remove it so it would wait forever (or until you turn your bot off). The check
parameter is also optional.
P.S. I am assuming you are using python 3 or more hence the F-strings
Upvotes: 3
Reputation: 418
Yes, that's possible for sure, there are multiple ways for doing this, I never tried it before since I didn't create any bots with reaction things but that should work.
using on_reaction_add
@client.event
async def on_reaction_add(reaction, user):
channel = client.get_channel('123')
if reaction.message.channel.id != channel.id:
return
else:
if reaction.emoji == "✅":
#Do something
print(user.name)
print(user.discriminator)
await channel.send(f"{user} has won the game")
Make sure to take a look at the on_reaction_add documentation and the reaction one
Upvotes: -1