【发布时间】:2019-12-31 13:00:48
【问题描述】:
假设一个类似这样的命令:
@bot.command()
async def test(ctx):
def check(r, u):
return u == ctx.message.author and r.message.channel == ctx.message.channel and str(r.emoji) == '✅'
await ctx.send("React to this with ✅")
try:
reaction, user = await bot.wait_for('reaction_add', timeout=300.0, check=check)
except asyncio.TimeoutError:
await ctx.send('Timeout')
else:
await ctx.send('Cool, thanks!')
如果用户在对消息做出实际反应之前多次发送相同的命令,是否有任何方法可以取消 wait_for?因此,机器人停止等待对先前发送的消息的反应,而只等待最后一条。
【问题讨论】:
-
我不确定什么时候调用这里的东西,但
bot.wait_for是一个协程,可以用.cancel()docs.python.org/3/library/asyncio-task.html#asyncio.Task.cancel取消它 -
是的,但我不知道如何获取对实际协程的引用以取消它:/
标签: python discord discord.py coroutine