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






Код, который вы ищете, - 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
взгляните на модуль asyncio