首页 > 解决方案 > discord.py 通过命令发送消息,该命令在设定的时间后计算有多少人“投票”了反应(自定义表情符号)

问题描述

我尝试了几天(甚至没有成功询问)反应取决于消息。我正在处理的命令是投票,在一定时间后通过精确计算一个反应相对于另一个反应的投票量来自动写入结果。我想使用自定义表情符号,但我不能,甚至尝试使用普通表情符号,我什至不计算它们,也不知道该怎么做。我可以用普通的表情符号来做到这一点,但是当我用自定义表情符号尝试相同的过程时,命令锁定了我该thumbsup = len ([await i.users (). Flatten () for i in message.reactions if str (i. emoji) == please] [0]) 如何解决?

代码:

@client.command(aliases=["crp"])
@commands.has_permissions(administrator=True)
async def conteggio_reazioni_personalizzate(ctx, *, proposta):
    favore = get_emoji(ctx.guild, "Favorevole")
    contro = get_emoji(ctx.guild, "Contrario")
    flore = get_emoji(ctx.guild, "Astenuto")
    message = await ctx.send(proposta)
    await message.add_reaction(favore)
    await message.add_reaction(contro)
    await message.add_reaction(flore)
    await asyncio.sleep(10)
    message = await ctx.channel.fetch_message(message.id)
    await message.remove_reaction(favore, client.user)
    await message.remove_reaction(contro, client.user)
    await message.remove_reaction(flore, client.user)
    thumbsup = len([await i.users().flatten() for i in message.reactions if str(i.emoji) == favore][0])
    thumbsdown = len([await i.users().flatten() for i in message.reactions if str(i.emoji) == contro][0])
    neutral = len([await i.users().flatten() for i in message.reactions if str(i.emoji) == flore][0])
    await ctx.send(f"{thumbsup} a favore , {thumbsdown} contro e {neutral} astenuti")

错误:

Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 442, in conteggio_reazioni_personalizzate
    thumbsup = len([await i.users().flatten() for i in message.reactions if str(i.emoji) == favore][0])
IndexError: list index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IndexError: list index out of range

标签: discord.py

解决方案


您拥有消息对象,因此您可以从该消息中获取所有反应,并像这样过滤您想要的反应。

reaction = get(message.reactions, emoji = )

然后你可以调用你得到的反应数量,并用这些数据做任何你想做的事情。

print(reaction.count)

PS我知道反应表情符号很难得到,所以我为你收集了竖起大拇指和不喜欢的表情!


推荐阅读