python 计算拇指向上和拇指向下React的数量

kpbwa7wx  于 2023-02-18  发布在  Python
关注(0)|答案(1)|浏览(100)

我正在尝试做一个机器人,用于在各种服务器上进行投票,而我原来的计票方法有一个重大缺陷,有了它,人们可以做出React,拿走React,然后多次重新添加,每次投票都会被统计,简而言之,它基本上允许人们进行多次投票,代码如下:

def check(reaction, user):
  return user != '808555253317894163' and str(reaction.emoji) in ['👍', '👎']

yay = 0
nay = 0
loop = 0

while loop == 0:
  try:
    reaction, user = await bot.wait_for('reaction_add', timeout=timeLimit, check=check)
    if reaction.emoji == '👍' and user != '808555253317894163':
      yay += 1
    if reaction.emoji == '👎' and user != '808555253317894163':
      nay += 1
  
except:
  await ctx.send(f'Yay: {yay}\nNay: {nay}')

  if yay > nay:
    await ctx.send('The vote comes out to yay!')
  elif yay < nay:
    await ctx.send('The vote comes out to nay!')
  elif yay == nay:
    await ctx.send('The vote is a tie!')
  
  loop = 1

我想改变它,这样在时间限制后,它只计算竖起大拇指和放下大拇指的React,然后从每个减1(考虑到机器人的React)。唯一的另一个帖子,这是接近我想要检查只有竖起大拇指的React。有人知道我怎么做吗?

xoshrz7s

xoshrz7s1#

用多米尼克在这篇文章中说的话,我找到了我的问题的答案:

while loop == 0:
    try:
      reaction, user = await bot.wait_for('reaction_add', timeout=timeLimit, check=check)
      
    except:
      # Find the poll message from its id
      msg = await vote_channel.fetch_message(pollID)
      
      # Count the amount of thumbs up reactions
      thumbUps = get(msg.reactions, emoji='👍')
      yay = thumbUps.count - 1

      # Count the amount of thumbs down reactions
      thumbDowns = get(msg.reactions, emoji='👎')
      nay = thumbDowns.count - 1

      loop = 1

相关问题