简体   繁体   English

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

[英]Actually waiting for a reaction with discord bot (Python)

I am trying to actually let my bot wait till it receives a reaction on its message and meanwhile do nothing.我试图让我的机器人等到它收到对其消息的反应,同时什么都不做。 Just waiting.只是在等待。 First as soon as it receives the reaction it should continue with the rest of the code.首先,一旦它收到反应,它就应该继续执行其余的代码。 Here is a (shitty) example code:这是一个(糟糕的)示例代码:

@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 ❌.")

Excecuting this code ends up in a big mess without the bot waiting in between the messages.执行此代码会导致一团糟,而机器人却没有在消息之间等待。 How can I implement that the bot waits for a reaction between every message without already sending all of the other ones?我如何实现机器人在不发送所有其他消息的情况下等待每条消息之间的反应?

I'd appreciate anyone's help我很感激任何人的帮助

There might be some problems with the way you check the emojis... I'm not really sure :/您检查表情符号的方式可能存在一些问题......我不太确定:/

Anyways here's what I got working:无论如何,这就是我的工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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