@@ -393,7 +393,7 @@ def __init__(
393393 self .waits : Dict [str , List ] = {}
394394 self .owner_ids : set [Snowflake_Type ] = set (owner_ids )
395395
396- self .async_startup_tasks : list [Coroutine ] = []
396+ self .async_startup_tasks : list [tuple [ Coroutine , Iterable [ Any ], dict [ str : Any ]] ] = []
397397 """A list of coroutines to run during startup"""
398398
399399 # callbacks
@@ -581,12 +581,14 @@ async def _async_wrap(_coro: Listener, _event: BaseEvent, *_args, **_kwargs) ->
581581 else :
582582 self .dispatch (events .Error (source = repr (event ), error = e ))
583583
584- wrapped = _async_wrap (coro , event , * args , ** kwargs )
585584 try :
586- return asyncio .create_task (wrapped , name = f"interactions:: { event .resolved_name } " )
585+ asyncio .get_running_loop ()
586+ return asyncio .create_task (
587+ _async_wrap (coro , event , * args , ** kwargs ), name = f"interactions:: { event .resolved_name } "
588+ )
587589 except RuntimeError :
588590 self .logger .debug ("Event loop is closed; queuing task for execution on startup" )
589- self .async_startup_tasks .append (wrapped )
591+ self .async_startup_tasks .append (( _async_wrap , ( coro , event , * args ), kwargs ) )
590592
591593 @staticmethod
592594 def default_error_handler (source : str , error : BaseException ) -> None :
@@ -901,7 +903,12 @@ async def astart(self, token: str | None = None) -> None:
901903 # run any pending startup tasks
902904 if self .async_startup_tasks :
903905 try :
904- await asyncio .gather (* self .async_startup_tasks )
906+ await asyncio .gather (
907+ * [
908+ task [0 ](* task [1 ] if len (task ) > 1 else [], ** task [2 ] if len (task ) == 3 else {})
909+ for task in self .async_startup_tasks
910+ ]
911+ )
905912 except Exception as e :
906913 self .dispatch (events .Error (source = "async-extension-loader" , error = e ))
907914 try :
@@ -982,10 +989,11 @@ def dispatch(self, event: events.BaseEvent, *args, **kwargs) -> None:
982989 ) from e
983990
984991 try :
992+ asyncio .get_running_loop ()
985993 _ = asyncio .create_task (self ._process_waits (event ))
986994 except RuntimeError :
987995 # dispatch attempt before event loop is running
988- self .async_startup_tasks .append (self ._process_waits (event ))
996+ self .async_startup_tasks .append (( self ._process_waits , (event ,), {} ))
989997
990998 if "event" in self .listeners :
991999 # special meta event listener
0 commit comments