【问题标题】:How do I check which reaction inthe message is higher in Discord.py?如何检查 Discord.py 中消息中的哪个反应更高?
【发布时间】:2022-01-21 12:37:05
【问题描述】:

所以基本上,我正在创建一个命令,让您为某事投票,5 分钟后它会检查哪个更高。

但问题是我不知道该怎么做。

到目前为止,这是我的代码:

@client.command()
async def strongy_boi(ctx, boi):
  if boi == "copper-golem-allay":
    mess = await ctx.send(":heart: = copper golem :blue_heart: = allay")
    await mess.add_reaction('❤️')
    await mess.add_reaction('????')
    await asyncio.sleep(5)
    #do code that check which is higher

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    您可以这样做的一种方法是通过discord.Message.reactions 访问消息反应并遍历它们,检查discord.Reaction.count 并与当前最高值进行比较。但是,要检查反应,需要缓存消息,这可以通过await fetch_message() 完成。请查看下面的修改后的代码和进一步的解释。

    @client.command()
    async def strongy_boi(ctx, boi):
      if boi == "copper-golem-allay":
        mess = await ctx.send("❤️ = copper golem ? = allay")
        await mess.add_reaction('❤️')
        await mess.add_reaction('?')
        await asyncio.sleep(5)
    
        # new code starts here #
    
        msg = await ctx.channel.fetch_message(mess.id) # 'Cache' the message
        # create variables to save the highest reactions
        highest_reaction = ""
        highest_reaction_number = 0
    
        # msg.reactions format: 
        # [<Reaction emoji='❤️' me=True count=2>, <Reaction emoji='?' me=True count=1>]
    
        for reaction in msg.reactions: # iterate through every reaction in the message
          if (reaction.count-1) > highest_reaction_number:
            # (reaction.count-1) discounts the bot's reaction
            highest_reaction = reaction.emoji
            highest_reaction_count = reaction.count-1
    
        await ctx.send(f"{highest_reaction} wins with {highest_reaction_count} votes!")
    

    其他链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多