【问题标题】:Wait for multiple reactions Discord.py等待多个反应 Discord.py
【发布时间】:2021-06-27 18:18:58
【问题描述】:

我的目标是制作一个小小的“轮盘赌”游戏。每个想玩的成员都应该能够对任何表情符号做出反应。但似乎我的代码只检测到第一反应。我想获得一个列表,哪个成员对哪个表情符号做出反应,以便稍后在代码中使用它。但现在的目标是为每个反应运行我的 IF 语句。希望你能明白我想要什么^^

@commands.command()
async def roulette(self, ctx, value: int):
    msg = await ctx.send(f"Choose a color. Make sure you have at least {value} Kakera in your wallet!")
    await msg.add_reaction("⚫")
    await msg.add_reaction("????")
    await msg.add_reaction("????")

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["⚫", "????", "????"]

    try:

        reaction, user = await self.bot.wait_for('reaction_add', timeout=30, check=check)

        await asyncio.sleep(5)
        async for user in reaction.users():
            await ctx.send('{0} has reacted with {1.emoji}!'.format(user, reaction))
        print()
        if str(reaction.emoji) == "⚫":
            await ctx.send(f"Black from {user}!")
        elif str(reaction.emoji) == "????":
            await ctx.send(f"Red from {user}!")
        elif str(reaction.emoji) == "????":
            await ctx.send(f"Green from {user}!")
        else:
            print("Unknown reaction")
    except asyncio.TimeoutError:
        await ctx.send("Abort execution!")

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    self.bot.wait_forreturns the first event that matches the criteria。在一个用户点击一个反应后,机器人会继续通过这部分代码,而您的机器人将不会响应其他人。

    您需要继续等待新的反应,直到收到 TimeoutError 或其他一些标准。您可以使用循环或其他事件,例如 on_reaction_add

    【讨论】:

    • 非常感谢!将与 on_reaction_add 事件一起去^^
    【解决方案2】:

    我想你可以试试,在命令内:

    @commands.command()
    async def roulette(self, ctx, value: int):
        ...
        
        @bot.event()
        async def on_reaction_add(reaction, user):
            # your logic here
    

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 1970-01-01
      • 2021-06-20
      • 2019-10-01
      • 2020-06-07
      • 2021-06-22
      • 2022-08-10
      • 1970-01-01
      • 2021-12-30
      相关资源
      最近更新 更多