требуется несколько wait_for_messages discord.py - PullRequest
0 голосов
/ 16 сентября 2018

По сути, я делаю тест, и я хочу иметь возможность искать ответы и определять, в каком сообщении есть только исполнитель, в каком сообщении только название песни, и в каком сообщении говорится о них обоих. Я сделал 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)))

1 Ответ

0 голосов
/ 17 сентября 2018

Код, который вы ищете: asyncio.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
...