【问题标题】:How do I count reactions on a message in discord py? [duplicate]如何计算对 discord py 中消息的反应? [复制]
【发布时间】:2021-10-22 08:25:52
【问题描述】:

我目前正在开发一个不和谐的机器人,我做了一个创建投票的命令。现在我希望机器人计算反应并根据反应量编辑消息(如果有更多 ✅ 或更多 ❌ 反应)。我阅读了 API 参考和多个网站,但找不到任何对我有帮助的东西。

这是我使用的代码:

    @commands.command(name="poll")
    async def poll(self,ctx: commands.Context,arg1, arg2):
        arg3 = int(arg2)/3600
        embed = discord.Embed(title="NEW POLL", description=f"{arg1} \nPoll lasts for: {round(arg3, 2)}h")
        embed2 = discord.Embed(title="POLL OVER", description="The answer is `yes`(✅)!")
        embed3 = discord.Embed(title="POLL OVER", description="The answer is `no` (❌)!")
        embed4 = discord.Embed(title="POLL OVER", description="It's a tie!")
        message = await ctx.send(embed=embed)
        await message.add_reaction("✅")
        await message.add_reaction("❌")
        time.sleep(int(arg2))
        

【问题讨论】:

标签: python pip discord discord.py


【解决方案1】:

这应该可行!

from discord.utils import get

@client.event
async def on_raw_reaction_add(ctx):
    if ctx.channel_id == channelidhere:
        if ctx.emoji.name == "✅":
            channel = client.get_channel(chanelidhere)
            message = await channel.fetch_message(ctx.message_id)
            reaction = get(message.reactions, emoji=ctx.emoji.name)
            COUNT = reaction.count

【讨论】:

  • 感谢您的回答。但是如何将它添加到命令中,使其不是新事件,而是命令的一部分。这在 python 中甚至可能吗?
【解决方案2】:

你可以试试:

most_voted = max(message.reactions, key=lambda r: r.count)

然后使用most_voted.emojimost_voted.count-1(-1 减去机器人的自我反应)分别获取该表情符号的表情符号和计数。

【讨论】:

    猜你喜欢
    • 2020-03-03
    • 2020-12-30
    • 1970-01-01
    • 2022-11-26
    • 2020-09-03
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多