Python是否可以在Discord.pyv1.0中等待一个或另一个事件?

2024-04-26 18:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

使用wait_for的方式是否可以让它等待reaction_addreaction_remove

我已经看到有on_reaction_addon_reaction_remove函数,但是我想要一种没有这些函数的方法

我想要这样的东西:

reaction,user=await bot.wait_for('reaction_add/reaction_remove',check=check)

Tags: 方法函数addforoncheckbot方式
1条回答
网友
1楼 · 发布于 2024-04-26 18:22:55

因为这看起来是在使用asyncio工具,所以使用内置的。只需为每个任务创建一个任务,然后使用^{}等待第一个任务启动:

pending_tasks = [bot.wait_for('reaction_add',check=check),
                 bot.wait_for('reaction_remove',check=check)]
done_tasks, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED)

当其中一个任务完成时,它将返回,并且满足的任务将出现在done_tasks集中。如果在其中一项任务完成后,您对其他任务不再感兴趣,您可以继续并取消其他任务,例如:

for task in pending_tasks:
    task.cancel()

相关问题 更多 >