Python - Is it possible to wait_for one event or another in Discord.py-v1.0?(Python - 是否可以在 Discord.py-v1.0 中等待一个事件或另一个事件?)
问题描述
是否有任何使用 wait_for
以等待 reaction_add
或 reaction_remove
的方式?
Is there any to use wait_for
in such a way that it will wait for either reaction_add
or reaction_remove
?
我已经看到有 on_reaction_add
和 on_reaction_remove
函数,但我想要一种没有这些的方法.
I've seen that there are on_reaction_add
and on_reaction_remove
functions, but I would like a way to do it without those.
我想要这样的东西:
reaction,user=await bot.wait_for('reaction_add/reaction_remove',check=check)
推荐答案
由于这看起来是在使用 asyncio
工具,所以使用内置插件.只需为每个任务创建一个任务,然后使用 asyncio.wait
等待第一个触发:
Since this looks to be using asyncio
facilities, use the built-ins. Just create a task for each, then use asyncio.wait
to wait for the first one to fire:
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
集中.如果您在其中一个任务完成后不再对其他任务感兴趣,您可以继续取消其他任务,例如:
When one of the tasks completes, this will return, and the task which was satisfied will appear in the done_tasks
set. If you're no longer interested in other tasks once one of them completes, you can go ahead and cancel the others, e.g.:
for task in pending_tasks:
task.cancel()
这篇关于Python - 是否可以在 Discord.py-v1.0 中等待一个事件或另一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python - 是否可以在 Discord.py-v1.0 中等待一个事件或另一个事件?


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01