Python 需要多条等待消息discord.py

Python 需要多条等待消息discord.py,python,discord,discord.py,Python,Discord,Discord.py,基本上我是在做一个测试,我希望能够搜索答案,并确定哪条消息只包含艺术家,哪条消息只包含歌曲名称,哪条消息同时包含他们。我制作了3个检查函数来显示这一点,但是我希望所有3个wait_for_消息语句并排运行。有什么办法可以补救吗 await client.say("What is the song name and artist?") def check1(msg): return name in msg.content.upper() and artist not in msg.cont

基本上我是在做一个测试,我希望能够搜索答案,并确定哪条消息只包含艺术家,哪条消息只包含歌曲名称,哪条消息同时包含他们。我制作了3个检查函数来显示这一点,但是我希望所有3个wait_for_消息语句并排运行。有什么办法可以补救吗

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)))

您正在查找的代码是。这允许您同时运行多个协同路由,并等待所有方法返回

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的重写版本有
客户端。wait_for
抛出错误而不是返回无,因此您可以改为执行此操作

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

查看asyncio moduleInstead,检查是否有符合这些标准的消息,然后将其分类到外部
wait_for_message
@PatrickHaugh我认为他正在等待这三个消息,这样,如果人们猜测的只是名称,其他人仍然可以尝试猜测艺术家或两者同时猜测。