【问题标题】:How to handle multiple reactions in Discord py (Bot)如何在 Discord py (Bot) 中处理多个反应
【发布时间】:2020-06-27 05:50:09
【问题描述】:

试图让我的机器人处理对消息的多个反应。

如果我只检查一个反应,我可以得到一个这样的版本:

reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkR)

但是当我检查多个反应(如纸和剪刀)时,代码根本不起作用。

我到处寻找有关此方面的帮助,但找不到任何在 Discord 重写后的内容。

任何帮助表示赞赏!

# test rps
@bot.command()
async def test(ctx):

    eb = await getEmbed(ctx, "Rock, Paper, Scissors", "", {}, "", "Choose one:", discord.Colour.gold())

    msg = await ctx.message.channel.send(embed = eb)
    channel = msg.channel
    for emoji in ('????', '????', "✂"):
        await msg.add_reaction(emoji)

    # now check for response
    def checkR(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) == '????'
    def checkP(reaction, user):
        print("in paper")
        return user == ctx.message.author and str(reaction.emoji) == '????'
    def checkS(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) == '✂'

    try:
        reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkR)
        reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkP)
        reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkS)
    except asyncio.TimeoutError:
        await embed(ctx, "Game timed out.")
        return
    else:
        # we got a reaction
        await embed(ctx, "GOT A REACTION")
        await discord.Message.delete(msg)
        pass

【问题讨论】:

    标签: python discord


    【解决方案1】:

    感谢我一直在寻找这个 这就是我想出的。

    创建消息并添加反应:

        @commands.command()
    async def pageturn(self, ctx):
        guildid = str(ctx.guild.id)
        userid = str(ctx.author.id)
        previuspage = '⬅️'
        nextpage = '➡️'
        page = 1
        msg = await ctx.send(f'page{page}')
        await msg.add_reaction(previuspage)
        await msg.add_reaction(nextpage)
    
        def checkforreaction(reaction, user):
            return str(user.id) == userid and str(reaction.emoji) in [previuspage,nextpage]
    

    #loops直到reaction_add超时

        loopclose = 0
        while loopclose == 0:
            try:
                reaction, user = await self.client.wait_for('reaction_add', timeout=5,check = checkforreaction)
                if reaction.emoji == nextpage:
                    page += 1
                elif reaction.emoji == previuspage:
                    page-= 1
                await msg.edit(content=f'page{page}')
    
            except asyncio.TimeoutError:
               await ctx.send('timeout')
               loopclose = 1
    

    【讨论】:

      【解决方案2】:
      reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkR)
      reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkP)
      reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkS)
      

      这等待有人按照 Rock、Paper、Scissor 的顺序做出反应。它不仅接受纸,也不接受剪刀。它需要所有 3 个反应并按此顺序。

      你需要这样写:

      def check(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) in ['?', '?', '✂']
      
      reaction, user = await bot.wait_for('reaction_add', timeout=5, check=check)
      

      这将寻找一种反应,即石头、纸或剪刀。

      【讨论】:

      • 好吧,这是有道理的......那么我怎么知道在 else 块中对哪个做出了反应,以便我可以做出相应的反应?
      • 信息在变量reaction 中,它是Reaction 类的一个实例:discordpy.readthedocs.io/en/latest/… 根据文档,您可以通过reaction.emoji 访问表情符号
      • 明白了。类似于:if(discord.Reaction.emoji == '?'):*do something*
      • 在这种情况下只是reaction.emoji 而不是discord.Reaction.emoji
      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 2021-11-10
      • 2021-06-17
      • 2021-11-22
      • 2021-06-04
      • 2022-01-24
      • 2020-06-29
      • 2021-05-12
      相关资源
      最近更新 更多