Fetch application_info on login

This allows users to no longer pass application_id when calling sync
inside setup_hook
This commit is contained in:
Rapptz
2022-05-22 19:52:50 -04:00
parent 06c43d6772
commit ee71366f72
3 changed files with 31 additions and 8 deletions

View File

@@ -238,6 +238,7 @@ class Client:
self._connection.shard_count = self.shard_count
self._closed: bool = False
self._ready: asyncio.Event = MISSING
self._application: Optional[AppInfo] = None
self._connection._get_websocket = self._get_websocket
self._connection._get_client = lambda: self
@@ -345,8 +346,9 @@ class Client:
"""Optional[:class:`int`]: The client's application ID.
If this is not passed via ``__init__`` then this is retrieved
through the gateway when an event contains the data. Usually
after :func:`~discord.on_connect` is called.
through the gateway when an event contains the data or after a call
to :meth:`~discord.Client.login`. Usually after :func:`~discord.on_connect`
is called.
.. versionadded:: 2.0
"""
@@ -360,6 +362,22 @@ class Client:
"""
return self._connection.application_flags
@property
def application(self) -> Optional[AppInfo]:
"""Optional[:class:`~discord.AppInfo`]: The client's application info.
This is retrieved on :meth:`~discord.Client.login` and is not updated
afterwards. This allows populating the application_id without requiring a
gateway connection.
This is ``None`` if accessed before :meth:`~discord.Client.login` is called.
.. seealso:: The :meth:`~discord.Client.application_info` API call
.. versionadded:: 2.0
"""
return self._application
def is_ready(self) -> bool:
""":class:`bool`: Specifies if the client's internal cache is ready for use."""
return self._ready is not MISSING and self._ready.is_set()
@@ -541,6 +559,13 @@ class Client:
data = await self.http.static_login(token.strip())
self._connection.user = ClientUser(state=self._connection, data=data)
self._application = await self.application_info()
if self._connection.application_id is None:
self._connection.application_id = self._application.id
if not self._connection.application_flags:
self._connection.application_flags = self._application.flags
await self.setup_hook()
async def connect(self, *, reconnect: bool = True) -> None:

View File

@@ -184,6 +184,7 @@ class ConnectionState:
self.shard_count: Optional[int] = None
self._ready_task: Optional[asyncio.Task] = None
self.application_id: Optional[int] = utils._get_as_snowflake(options, 'application_id')
self.application_flags: ApplicationFlags = utils.MISSING
self.heartbeat_timeout: float = options.get('heartbeat_timeout', 60.0)
self.guild_ready_timeout: float = options.get('guild_ready_timeout', 2.0)
if self.guild_ready_timeout < 0: