计算discord.py上的反应

2024-04-25 20:52:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个投票机器人,现在我想知道如何计算反应并找到赢家的答案,我已经尝试了很多,但我无法做到这一点。有人知道吗

embed = discord.Embed(title="¡Es momento de tomar una encuesta!", colour=discord.Colour(0x3e038c))
embed.add_field(name='Autor de la encuesta', value=f'{ctx.author}')
embed.add_field(name='Pregunta:', value=f'{pregunta}')
msg = await ctx.send(embed=embed)
await msg.add_reaction(str('🔺'))
await msg.add_reaction(str('🔻'))

Tags: nameaddfieldvalue机器人demsgembed
1条回答
网友
1楼 · 发布于 2024-04-25 20:52:25

要实现您想要的,您只需要检查message.reactions参数的length

例如:

from discord.utils import get

async def check_winner(self, ctx, messageid: int):
    # ...
    # Some code here

    message = await ctx.fetch_message(messageid)
    reaction = get(message.reactions, emoji='your emoji here')
    num_reactions = reaction.count

    # More code here
    # ...

在检查投票获胜者的命令中,您首先需要检索投票的消息\u id,然后将其作为参数传递给您的命令,以便检索message对象并查找附加到它的反应

参考资料:

相关问题 更多 >