python - 在 discord.py 中等待
问题描述
我正在制作一个计算器命令,我制作了这段代码
@commands.command(brief="Test")
async def calc(self, ctx, num1: int, num2: int):
# TODO: Make this look better
reaction = [
'\U0001f1e6',
'\U0001f1f8',
'\U0001f1f2',
'\U0001f1e9'
]
embed=discord.Embed(title="Calculator", description="Chose one of the following Operations \n \n1)Addition (+) \n2)Subbraction (-)\n3)Multiplication (x)\n4)Division (/)", color=0x93e6fb)
embed.set_footer(text=f"React with the emoji you want as Operator")
message = await ctx.send(embed=embed)
for i in reaction:
await message.add_reaction(i)
def check(reaction, user):
return str(reaction.emoji) == '\U0001f1e6', '\U0001f1f8', '\U0001f1f2', '\U0001f1e9' and reaction.message == message and user == ctx.author
try:
reaction, user = await self.bot.wait_for('reaction_add', timeout=10.0, check=check)
except asyncio.TimeoutError:
await ctx.message.delete()
await message.delete()
else:
if str(reaction.emoji) == '\U0001f1e6':
value = num1 + num2
print(value)
await ctx.send(value)
elif str(reaction.emoji) == '\U0001f1f8':
value = num1 - num2
print(value)
await ctx.send(value)
elif str(reaction.emoji) == '\U0001f1f2':
value = num1 * num2
print(value)
await ctx.send(value)
elif str(reaction.emoji) == '\U0001f1e9':
value = num1 / num2
print(value)
await ctx.send(value)
当我有时使用该命令时,它默认为最后一个 Elif 语句,有时这可以完美运行 请提前帮助谢谢。
解决方案
这个
str(reaction.emoji) == '\U0001f1e6', '\U0001f1f8', '\U0001f1f2', '\U0001f1e9'
不是一个有效的python语句,如果你想比较一个反应是否是你可以使用列表和in
关键字中的任何一个
str(reaction) in ['\U0001f1e6', '\U0001f1f8', '\U0001f1f2', '\U0001f1e9'] # Also you can use the `reactions` list you defined before
推荐阅读
- r - 在ggplot2中不同形状的点之间画一条线
- algorithm - 如何找到包含图子集中所有节点的图的最小电路?
- amazon-web-services - 如何配置路由 53 以在浏览器中维护域名以进行 URL 重定向?
- r - 如何从列表中制作数据框?
- nullpointerexception - 布尔方法在调用时返回 Null
- java - JavaFX 表不显示数据
- c# - TabControl 自定义标签栏背景颜色
- r - 我正在尝试添加阿肯色州的州线。我还希望在滚动时显示邮政编码。我正在使用 ggplot
- java - 如何在 Java、XML 文件中显示具有特定 id 和标签名称以及特定父节点下的节点的值?
- postman - postman获取图片验证码乱码怎么解决