Python-在Discord.py-v1.0中是否可以等待一个事件或另一个事件? [英] Python - Is it possible to wait_for one event or another in Discord.py-v1.0?

查看:79
本文介绍了Python-在Discord.py-v1.0中是否可以等待一个事件或另一个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以等待 reaction_add reaction_remove 的方式使用 wait_for ?

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.等待 等待第一个触发:

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中是否可以等待一个事件或另一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆