Reputation: 21
I'm doing a command where you need to confirm something with emojis.
I have a wait_for("reaction_add")
with a check as a lambda function.
The following code I have is:
try:
reaction, user = await self.client.wait_for("reaction_add",
check=lambda react, usr: str(reaction.emoji) == "✅" and usr.id == ctx.author.id, timeout=60)
print(reaction.emoji)
except asyncio.TimeoutError:
await confirm_msg.edit(content="This message has timed out!", embed=None)
But it doesn't print out the reaction emoji. Without the check the code works fine, so it has to do with the check. How could I fix it?
Thanks!
Upvotes: 1
Views: 192
Reputation: 442
You can try this, without the lambda function:
@client.event
async def on_raw_reaction_add(payload):
reaction = str(payload.emoji)
if reaction == "✅" and usr.id == ctx.author.id:
print('do something')
else:
print('something else')
Upvotes: 0
Reputation: 2907
try:
reaction, user = await self.client.wait_for("reaction_add",
check=lambda reaction, user: str(reaction.emoji) == "✅" and user.id == ctx.author.id, timeout=60)
print(reaction.emoji)
except asyncio.TimeoutError:
await confirm_msg.edit(content="This message has timed out!", embed=None)
Upvotes: 1
Reputation: 2429
A lambda function is esentially the same as a normal function.
Your lambda:
lambda react, usr: str(reaction.emoji) == "✅" and usr.id == ctx.author.id
Would be equal to defining the following function:
# Here we are within the wait_for(reaction_add)
def f(react, usr):
return str(reaction.emoji) == "✅" and user.id == ctx.author.id
# Rest of the code
The problem is that the reaction_add
does not have react
or usr
defined. The way to solve your code would be something like this:
reaction, user = await self.client.wait_for("reaction_add", check=lambda reaction,
user: str(reaction.emoji) == "✅" and user.id == ctx.author.id, timeout=60)
Upvotes: 1