discord.py通过命令发送一条消息,该命令在设置的时间后计算有多少“投票”了一个反应(自定义表情符号)

时间:2020-08-29 22:27:04

标签: 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

1 个答案:

答案 0 :(得分:0)

您拥有message对象,因此您可以从该消息中获取所有响应,并过滤所需的响应。

reaction = get(message.reactions, emoji = ?)

然后,您可以调用获得的反应数量,并对该数据进行任何所需的操作。

print(reaction.count)

PS,我知道反应表情符号很难获得,所以我为您收集了大拇指和大拇指朝下的信息! ?