实际上在等待与不和谐机器人(Python)的反应

时间:2021-03-21 02:13:58

标签: python events discord.py bots python-asyncio

我实际上试图让我的机器人等到它收到对其消息的反应,同时什么都不做。只是在等待。首先,一旦它收到反应,它就应该继续执行其余的代码。这是一个(糟糕的)示例代码:

@client.command()
async def test(ctx):
    await ctx.send("waiting for players to join ...")
    for i in range(5):
        botMsg = await ctx.send("User X do you want to play?")
        await botMsg.add_reaction("✔️")
        await botMsg.add_reaction("❌")

        try:
            reaction, player = await client.wait_for('reaction_add', timeout=20, check=lambda reaction, player: reaction.emoji in ["✔️", "❌"])
        except asyncio.TimeoutError:
            await ctx.send("No one reacted.")

        if client.user != player and reaction.emoji == "✔️":
            await ctx.send(f"{player.mention} reacted with ✔️.")
        elif client.user != player and reaction.emoji == "❌":
            await ctx.send(f"{player.mention} reacted with ❌.")

执行此代码会导致一团糟,而机器人却没有在消息之间等待。如何实现机器人在不发送所有其他消息的情况下等待每条消息之间的反应?

感谢任何人的帮助

1 个答案:

答案 0 :(得分:0)

您检查表情符号的方式可能存在一些问题...我不太确定:/

无论如何,这就是我的工作:

@client.command()
async def test(ctx):
    for i in range(5):
        msg = await ctx.send("Waiting for reactions...")
        emojis = [u"\u2714", u"\u274C"]
        await msg.add_reaction(emojis[0])
        await msg.add_reaction(emojis[1])
        
        try:
            reaction, user = await client.wait_for('reaction_add', timeout=20.0, check=lambda reaction, user: user.id != client.user.id and reaction.emoji in emojis)
            if reaction.emoji == emojis[0]:
                await ctx.send(f"{user.mention} said yes!")
            else:
                await ctx.send(f"{user.mention} said no :(")
        except asyncio.TimeoutError:
            await ctx.send("You\'re out of time!")