我正在做一个命令,你需要用表情符号确认一些东西。我有一个wait_for("reaction_add")
,检查作为lambda函数。
下面的代码是:
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)
但是它不会打印出反应的表情符号。没有检查代码也能正常工作,所以它必须与检查有关。我怎样才能修好它呢?
谢谢!
Alambda函数本质上与普通函数相同。
λ:
lambda react, usr: str(reaction.emoji) == "✅" and usr.id == ctx.author.id
等于定义以下函数:
# 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
问题是reaction_add
没有定义react
或usr
。解决您的代码的方法是这样的:
reaction, user = await self.client.wait_for("reaction_add", check=lambda reaction,
user: str(reaction.emoji) == "✅" and user.id == ctx.author.id, timeout=60)
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)
您可以尝试不使用lambda函数:
@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')