python - 多个 wait_for_messages 想要discord.py

标签 python discord discord.py

基本上,我正在做一个测验,我希望能够搜索答案并确定哪条消息中只有艺术家,哪条消息中只有歌曲名称,以及哪条消息中都包含这两个内容。我已经创建了 3 个检查函数来显示这一点,但是我希望所有 3 个 wait_for_message 语句并排运行。关于如何解决这个问题有什么想法吗?

await client.say("What is the song name and artist?")
def check1(msg):
    return name in msg.content.upper() and artist not in msg.content.upper()
def check2(msg):
    return artist in msg.content.upper() and name not in msg.content.upper()
def check3(msg):
    return name in msg.content.upper() and artist in msg.content.upper()
msg1 = await client.wait_for_message(timeout=10, check=check1)
msg2 = await client.wait_for_message(timeout=10, check=check2)
msg3 = await client.wait_for_message(timeout=20, check=check3)
if msg3 is not None:
   await client.say("@{} got both of them right! It was indeed {} by {}".format(msg3.author, toString(name), 
                                                                                     toString(artist)))
elif msg1 is not None and msg2 is not None:
        await client.say("@{} got the song name and @{} got the artist name! It was indeed {} by {}".format(msg1.author, 
                                                                           msg2.author, toString(name), toString(artist)))
elif msg1 is not None and msg2 is None:
        await client.say("@{} got the song name but no one got the artist! It was {} by {}".format(msg1.author,
                                                                                       toString(name), toString(artist)))
elif msg1 is None and msg2 is not None:
        await client.say("@{} got the artist name but no one got the song name! It was {} by {}".format(msg2.author,
                                                                                       toString(name), toString(artist)))
elif msg1 is None and msg2 is None and msg3 is None:
        await client.say("No one got it! It was {} by {}! Better luck next time".format(toString(name), toString(artist)))

最佳答案

您要查找的代码是 asyncio.gather 。这使您可以同时运行多个协程并等待所有方法返回。

gather 的返回列表按照输入的顺序排列,而不是按照任务完成的顺序排列。

ret = await asyncio.gather(
    client.wait_for_message(timeout=10, check=check1),
    client.wait_for_message(timeout=10, check=check2),
    client.wait_for_message(timeout=10, check=check3)
)

msg1, msg2, msg3 = *ret
# msg1 has the name
# msg2 has the artist
# msg3 has both

由于 discord.py 的重写版本 client.wait_for 抛出错误而不是返回 None,因此您可以这样做。

ret = await asyncio.gather(
    client.wait_for("message", timeout=10, check=check1),
    client.wait_for("message", timeout=10, check=check2),
    client.wait_for("message", timeout=10, check=check3),
    return_exceptions = True
)

# Test for errors
ret = [r if not isinstance(r, Exception) else None for r in ret]
msg1, msg2, msg3 = *ret

关于python - 多个 wait_for_messages 想要discord.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52358080/

相关文章:

python - 如何将 discord.py help 命令放入嵌入中?

discord.py - 如何创建一个 Discord 机器人来创建包含视频预览的嵌入内容

python-3.x - Discord.py 计算一条消息的 react

python - 使用 python 创建 Discord Webhook

python - 如何比较系列中的两个值,而不是系列对象? Python 3.x

python - 验证和格式化 JSON 文件

javascript - 有没有办法识别用户如何阅读帖子

python - 如何在 Django 中使用外键?

javascript - Discord.js Tenor API 错误 [显示来自嵌入的图像]

c# - Discord.NET 向 SocketMessage 添加 react