如何检查 Discord.py 中消息中的哪个反应更高?

How do I check which reaction inthe message is higher in Discord.py?

所以基本上,我正在创建一个命令,让您为某件事投票,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

执行此操作的一种方法是通过 discord.Message.reactions and iterate through them, checking the discord.Reaction.count and comparing with the current highest. To check for reactions, however, a message needs to be cached, which can be done through 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!")

其他链接:

  • - Whosebug
  • How do I count reactions on a message in discord py? - Whosebug
  • - Whosebug
  • Get a List of Reactions on a Message - Whosebug