简体   繁体   中英

Wait for in discord.py

I am making a calculator command and I made this code

@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)

when I use the command some times it is defaulting to the last Elif statement and sometimes this works flawlessly Please Help Thanks in advance.

This

str(reaction.emoji) == '\U0001f1e6', '\U0001f1f8', '\U0001f1f2', '\U0001f1e9'

Is not a valid python statement, if you want to compare if a reaction is any of those you can use a list and the in keyword

str(reaction) in ['\U0001f1e6', '\U0001f1f8', '\U0001f1f2', '\U0001f1e9'] # Also you can use the `reactions` list you defined before

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM