Browse Source

Forbid wait_until_ready inside setup_hook

pull/7706/head
apple502j 3 years ago
committed by GitHub
parent
commit
3e77a7b29e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      discord/client.py

21
discord/client.py

@ -29,6 +29,7 @@ import datetime
import logging
import sys
import traceback
from contextvars import ContextVar
from typing import (
Any,
AsyncIterator,
@ -95,6 +96,8 @@ __all__ = (
Coro = TypeVar('Coro', bound=Callable[..., Coroutine[Any, Any, Any]])
_inside_setup_hook: ContextVar[bool] = ContextVar('_inside_setup_hook', default=False)
_log = logging.getLogger(__name__)
@ -482,6 +485,11 @@ class Client:
any events are dispatched, making it a better solution than doing such
setup in the :func:`~discord.on_ready` event.
.. warning::
Calling :meth:`wait_until_ready` inside this coroutine raises
an exception to prevent a deadlock.
.. versionadded:: 2.0
"""
pass
@ -518,7 +526,11 @@ class Client:
data = await self.http.static_login(token.strip())
self._connection.user = ClientUser(state=self._connection, data=data)
await self.setup_hook()
try:
ctx_token = _inside_setup_hook.set(True)
await self.setup_hook()
finally:
_inside_setup_hook.reset(ctx_token)
async def connect(self, *, reconnect: bool = True) -> None:
"""|coro|
@ -949,7 +961,14 @@ class Client:
"""|coro|
Waits until the client's internal cache is all ready.
Raises
-------
ClientException
If this coroutine was called inside :meth:`setup_hook`.
"""
if _inside_setup_hook.get():
raise ClientException('wait_until_ready cannot be called inside setup_hook.')
if self._ready is not MISSING:
await self._ready.wait()

Loading…
Cancel
Save