Search code examples
discorddiscord.pynextcordpycord

How to view multiple events with bot.wait_for?


I want to listen for many events with bot.wait_for and not a single event. First event blocks the second one when I arrange them back to back.


Solution

  • done, pending = await asyncio.wait([
                        bot.loop.create_task(bot.wait_for('message')),
                        bot.loop.create_task(bot.wait_for('reaction_add'))
                    ], return_when=asyncio.FIRST_COMPLETED)
    
    try:
        stuff = done.pop().result()
    except ...:
        # If the first finished task died for any reason,
        # the exception will be replayed here.
    for future in done:
        # If any exception happened in any other done tasks
        # we don't care about the exception, but don't want the noise of
        # non-retrieved exceptions
        future.exception()
    
    for future in pending:
        future.cancel()  # we don't need these anymore