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:UsersPC GIUSEPPEPycharmProjectsLMIIBot Developmentvenvlibsite-packagesdiscordextcommandscore.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:UsersPC GIUSEPPEPycharmProjectsLMIIBot Developmentvenvlibsite-packagesdiscordextcommandsbot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:UsersPC GIUSEPPEPycharmProjectsLMIIBot Developmentvenvlibsite-packagesdiscordextcommandscore.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersPC GIUSEPPEPycharmProjectsLMIIBot Developmentvenvlibsite-packagesdiscordextcommandscore.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

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

reaction = get(message.reactions, emoji =   )

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

print(reaction.count)

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

相关内容

最新更新