【问题标题】:How to count reactions on a post with a discord bot如何使用不和谐机器人计算帖子的反应
【发布时间】:2019-02-13 18:07:22
【问题描述】:

这是我的代码:

import discord
import asyncio

channel = None

async def ex(args, message, client, invoke):

    global channel
    channel = message.channel
    one = "????"
    two = "????"
    three = "????"

    one_count = 0
    two_count = 0
    three_count = 0

    vote = discord.Embed(title="**[POLL]**", description=" ", color=0x00ff00)
    vote.add_field(name="Please vote for the match type:", value="- "+one+" Solos\n- "+two+" Duos\n- "+three+" Squads", inline=True)
    message_1 = await client.send_message(channel, embed=vote)

    await client.add_reaction(message_1, one)
    await client.add_reaction(message_1, two)
    await client.add_reaction(message_1, three)

关于如何让它计算短暂等待后的反应量有什么想法吗?

谢谢

【问题讨论】:

  • 那篇文章使用了不和谐的重写,而不是我正在使用的
  • {react.emoji: react.count for react in message.reactions} 应该适用于两者。然后只需从该字典中访问您感兴趣的表情符号。
  • 我不明白如何在上下文中使用它,你能给我举个例子吗?

标签: python-3.x discord.py


【解决方案1】:

使用asyncio.await 等待投票结束,然后您可以获得所有反应的计数并以此来确定获胜者

async def ex(args, message, client, invoke):  # I don't understand these arguments, they seem unneccessary

    channel = message.channel
    choices = {"?": "Solos",
               "?": "Duos",
               "?": "Squads"}

    vote = discord.Embed(title="**[POLL]**", description=" ", color=0x00ff00)
    value = "\n".join("- {} {}".format(*item) for item in choices.items()) 
    vote.add_field(name="Please vote for the match type:", value=value, inline=True)

    message_1 = await client.send_message(channel, embed=vote)

    for choice in choices:
        await client.add_reaction(message_1, choice)

    await aynsio.sleep(60)  # wait for one minute
    message_1 = await client.get_message(channel, message_1.id)


    counts = {react.emoji: react.count for react in message_1.reactions}
    winner = max(choices, key=counts.get)

    await client.send_message(channel, "{} is the winner".format(choices[winner]))

【讨论】:

  • 返回错误? winner = max(choices, key=counts.get) TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'
  • @BenjaminJones 看起来我们已经引用的message_1 对象没有更新以反映对它的反应。我们可以通过get_message 重新获取Message 对象来解决这个问题,利用我们知道它是id 的事实。
猜你喜欢
  • 2021-03-03
  • 1970-01-01
  • 2021-02-16
  • 2019-07-10
  • 2021-07-13
  • 2020-05-25
  • 2020-12-31
  • 2021-08-07
  • 2021-07-07
相关资源
最近更新 更多