【问题标题】:Actually waiting for a reaction with discord bot (Python)实际上正在等待不和谐机器人的反应(Python)
【发布时间】:2021-06-18 01:07:32
【问题描述】:

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

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

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

感谢任何人的帮助

【问题讨论】:

  • 您可以简单地编辑原始消息,如果这是您想要的?

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2021-10-30
    • 2021-05-27
    • 2021-07-15
    • 2021-05-20
    • 2021-05-10
    • 2017-04-29
    • 1970-01-01
    相关资源
    最近更新 更多