首页 > 解决方案 > 在 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 语句,有时这可以完美运行 请提前帮助谢谢。

标签: pythondiscorddiscord.py

解决方案


这个

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

推荐阅读