【问题标题】:Count amount of thumbs up and thumbs down reactions计算竖起大拇指和竖起大拇指的反应
【发布时间】:2021-04-01 05:22:00
【问题描述】:

我正在尝试制作一个用于在各种服务器上进行投票的机器人,而我最初的计票方法存在重大缺陷。有了它,人们可以做出反应,把反应拿走,然后重新添加多次,每次都会计票。简而言之,它基本上允许人们投多张选票。代码如下:

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

我想更改它,以便在时间限制之后,它只计算拇指向上和向下反应的数量,然后从每个中减去 1(以考虑机器人的反应)。唯一一个接近我想要检查的只有竖起大拇指的反应的帖子。有人知道我该怎么做吗?

【问题讨论】:

  • 它现在在做什么?与您的预期有何不同?
  • 每当有人添加反应时,它都会检查他们添加的反应类型,然后根据他们选择竖起大拇指还是不喜欢来增加“是”或“否”。
  • 请从intro tour 重复on topichow to ask。 “告诉我如何解决这个编码问题”不是堆栈溢出问题。我们希望您做出诚实的尝试,然后然后就您的算法或技术提出一个具体的问题。 Stack Overflow 并不打算取代现有的文档和教程。计数是一项基本的语言技能,而不是 Stack Overflow 问题。
  • 我想找出某个表情符号的反应量,当我尝试研究时,没有对我有帮助的答案。我不再试图实时计算反应。我正在尝试分别计算赞成和反对的反应数量,因为如果您不知道人们投票给了什么,那么知道总票数是没有用的。
  • 您是否尝试过获取反应?不久前我回复了另一个post,也许有帮助。

标签: python discord discord.py


【解决方案1】:

使用 Dominik 在this post 中所说的话,我找到了问题的答案:

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多