Skip to content

Commit c453b2a

Browse files
committed
fix: prevent runtime warnings on startup when bot isnt allowed to start
1 parent 31e4636 commit c453b2a

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

interactions/client/client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

interactions/models/internal/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __new__(cls, bot: "Client", *args, **kwargs) -> "Extension":
124124

125125
if hasattr(instance, "async_start"):
126126
if inspect.iscoroutinefunction(instance.async_start):
127-
bot.async_startup_tasks.append(instance.async_start())
127+
bot.async_startup_tasks.append((instance.async_start, (), {}))
128128
else:
129129
raise TypeError("async_start is a reserved method and must be a coroutine")
130130

0 commit comments

Comments
 (0)