Skip to content

Commit 68a929f

Browse files
committed
make start also update loop if not already updated via __aenter__
1 parent eb113b0 commit 68a929f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

discord/client.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def __init__(
301301
self._connection._get_websocket = self._get_websocket
302302
self._connection._get_client = lambda: self
303303
self._event_handlers: dict[str, list[Coro]] = {}
304+
self._in_context: bool = False
304305

305306
if VoiceClient.warn_nacl:
306307
VoiceClient.warn_nacl = False
@@ -310,6 +311,7 @@ def __init__(
310311
self._tasks = set()
311312

312313
async def __aenter__(self) -> Client:
314+
self._in_context = True
313315
if self._loop is None:
314316
try:
315317
self._loop = asyncio.get_running_loop()
@@ -337,6 +339,7 @@ async def __aexit__(
337339
exc_v: BaseException | None,
338340
exc_tb: TracebackType | None,
339341
) -> None:
342+
self._in_context = False
340343
if not self.is_closed():
341344
await self.close()
342345

@@ -867,7 +870,20 @@ async def start(self, token: str, *, reconnect: bool = True) -> None:
867870
TypeError
868871
An unexpected keyword argument was received.
869872
"""
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+
871887
await self.login(token)
872888
await self.connect(reconnect=reconnect)
873889

0 commit comments

Comments
 (0)