【发布时间】:2020-05-29 19:26:55
【问题描述】:
我想向我的机器人添加一个投票命令,该命令接受一个opt1 和opt2 参数,添加两个反应到嵌入,并且用户有expir 时间投票,然后它发送一条消息哪个选项有更多票,但我有一个错误:RuntimeError: coroutine 'poll.<locals>.whowin' was never awaited。我不知道如何解决它!我尝试了一些解决方案,但没有奏效。这是我的代码:
@commands.cooldown(1, 45, commands.BucketType.guild)
async def poll(ctx, title, opt1, opt2, expir):
try:
expir = float(expir)
if expir > 120:
await ctx.send('Expiration time cannot be greater than 120 seconds.')
return False
except:
await ctx.send('Error while converting `expir` parameter.')
return False
pollEmbed=discord.Embed(
title=title,
description='React with role below:',
colour=discord.Colour.blue()
)
pollEmbed.add_field(name='Option 1', value=opt1)
pollEmbed.add_field(name='Option 2', value=opt2)
thisEmbed = await ctx.send(embed=pollEmbed)
await discord.Message.add_reaction(thisEmbed, emoji='1\U000020e3')
await discord.Message.add_reaction(thisEmbed, emoji='2\U000020e3')
async def whowin():
if len(users1) > len(users2):
await ctx.send('People most voted for: {0}'.format(opt1))
return True
elif len(users1) < len(users2):
await ctx.send('People most voted for: {0}'.format(opt2))
return True
else:
await ctx.send('There\'s a tie!')
return True
pollTimer = Timer(expir, whowin)
pollTimer.start()
@bot.event
async def on_reaction_add(reaction, user):
if discord.Reaction.message == thisEmbed:
if discord.Reaction.emoji == '1\U000020e3':
users1.append(user)
else:
users2.append(user)
虽然我知道第二个错误 (NameError) 的答案:我尝试将 thisEmbed 设为全局。这暂时修复了它,但仍然有RuntimeError。
【问题讨论】:
-
是的,但是这个问题很久以前就被问过了,我已经解决了。无论如何我都不使用该机器人,但感谢您提供指向已回答问题的链接。
标签: python discord.py