@@ -301,6 +301,7 @@ def __init__(
301
301
self ._connection ._get_websocket = self ._get_websocket
302
302
self ._connection ._get_client = lambda : self
303
303
self ._event_handlers : dict [str , list [Coro ]] = {}
304
+ self ._in_context : bool = False
304
305
305
306
if VoiceClient .warn_nacl :
306
307
VoiceClient .warn_nacl = False
@@ -310,6 +311,7 @@ def __init__(
310
311
self ._tasks = set ()
311
312
312
313
async def __aenter__ (self ) -> Client :
314
+ self ._in_context = True
313
315
if self ._loop is None :
314
316
try :
315
317
self ._loop = asyncio .get_running_loop ()
@@ -337,6 +339,7 @@ async def __aexit__(
337
339
exc_v : BaseException | None ,
338
340
exc_tb : TracebackType | None ,
339
341
) -> None :
342
+ self ._in_context = False
340
343
if not self .is_closed ():
341
344
await self .close ()
342
345
@@ -867,7 +870,20 @@ async def start(self, token: str, *, reconnect: bool = True) -> None:
867
870
TypeError
868
871
An unexpected keyword argument was received.
869
872
"""
870
- # Update the loop to get the running one in case the one set is MISSING
873
+ if not self ._in_context and self ._loop is None :
874
+ # Update the loop to get the running one in case the one set is MISSING
875
+ try :
876
+ self ._loop = asyncio .get_running_loop ()
877
+ except RuntimeError :
878
+ self ._loop = asyncio .new_event_loop ()
879
+
880
+ self ._pending_loops .start (self )
881
+ self .http .loop = self .loop
882
+ self ._connection .loop = self .loop
883
+
884
+ self ._ready = asyncio .Event ()
885
+ self ._closed = asyncio .Event ()
886
+
871
887
await self .login (token )
872
888
await self .connect (reconnect = reconnect )
873
889
0 commit comments