Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework login-flow to retrieve user object from appinfo #9499

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions discord/appinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Team as TeamPayload,
InstallParams as InstallParamsPayload,
)
from .types.user import User as UserPayload
from .user import User
from .state import ConnectionState

Expand Down Expand Up @@ -142,6 +143,10 @@ class AppInfo:
redirect_uris: List[:class:`str`]
A list of authentication redirect URIs.

.. versionadded:: 2.4
bot: Optional[:class:`User`]
The bot user, if this application belongs to a bot.

.. versionadded:: 2.4
"""

Expand All @@ -153,6 +158,7 @@ class AppInfo:
'rpc_origins',
'bot_public',
'bot_require_code_grant',
'bot',
'owner',
'_icon',
'verify_key',
Expand Down Expand Up @@ -188,6 +194,9 @@ def __init__(self, state: ConnectionState, data: AppInfoPayload):
team: Optional[TeamPayload] = data.get('team')
self.team: Optional[Team] = Team(state, team) if team else None

bot: Optional[UserPayload] = data.get('bot')
self.bot: Optional[User] = state.create_user(bot) if bot else None

self.verify_key: str = data['verify_key']

self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id')
Expand Down
4 changes: 2 additions & 2 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ async def login(self, token: str) -> None:
token = token.strip()

data = await self.http.static_login(token)
self._connection.user = ClientUser(state=self._connection, data=data)
self._application = await self.application_info()
self._connection.user = ClientUser(state=self._connection, data=data['bot']) # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it would be better to raise above here if 'bot' not in data.
I don't think it can ever happen though.

self._application = AppInfo(self._connection, data)
if self._connection.application_id is None:
self._connection.application_id = self._application.id

Expand Down
4 changes: 2 additions & 2 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ async def close(self) -> None:

# login management

async def static_login(self, token: str) -> user.User:
async def static_login(self, token: str) -> appinfo.AppInfo:
# Necessary to get aiohttp to stop complaining about session creation
if self.connector is MISSING:
# discord does not support ipv6
Expand All @@ -807,7 +807,7 @@ async def static_login(self, token: str) -> user.User:
self.token = token

try:
data = await self.request(Route('GET', '/users/@me'))
data = await self.application_info()
except HTTPException as exc:
self.token = old_token
if exc.status == 401:
Expand Down
1 change: 1 addition & 0 deletions discord/types/appinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class AppInfo(BaseAppInfo):
owner: User
bot_public: bool
bot_require_code_grant: bool
bot: NotRequired[User]
team: NotRequired[Team]
guild_id: NotRequired[Snowflake]
primary_sku_id: NotRequired[Snowflake]
Expand Down
Loading