diff --git a/client/src/telethon/_impl/client/client/auth.py b/client/src/telethon/_impl/client/client/auth.py index 20a859af..6a66c7ac 100644 --- a/client/src/telethon/_impl/client/client/auth.py +++ b/client/src/telethon/_impl/client/client/auth.py @@ -30,9 +30,7 @@ async def complete_login(client: Client, auth: abcs.auth.Authorization) -> User: assert isinstance(auth.user, types.User) user = User._from_raw(auth.user) client._config.session.user = SessionUser( - id=user.id, - dc=client._dc_id, - bot=user.bot, + id=user.id, dc=client._dc_id, bot=user.bot, username=user.username ) packed = user.pack() diff --git a/client/src/telethon/_impl/client/client/net.py b/client/src/telethon/_impl/client/client/net.py index c1516a4a..41fd04c8 100644 --- a/client/src/telethon/_impl/client/client/net.py +++ b/client/src/telethon/_impl/client/client/net.py @@ -12,6 +12,7 @@ from ...mtsender import Sender from ...mtsender import connect as connect_without_auth from ...mtsender import connect_with_auth from ...session import DataCenter, Session +from ...session import User as SessionUser from ...tl import LAYER, Request, functions from .updates import dispatcher, process_socket_updates @@ -149,6 +150,16 @@ async def connect(self: Client) -> None: self._config.session.user = None except Exception as e: pass + else: + if not self._config.session.user: + me = await self.get_me() + assert me is not None + self._config.session.user = SessionUser( + id=me.id, dc=self._dc_id, bot=me.bot, username=me.username + ) + packed = me.pack() + assert packed is not None + self._chat_hashes.set_self_user(packed) self._dispatcher = asyncio.create_task(dispatcher(self)) diff --git a/client/src/telethon/_impl/session/session.py b/client/src/telethon/_impl/session/session.py index 3a584389..a4091a69 100644 --- a/client/src/telethon/_impl/session/session.py +++ b/client/src/telethon/_impl/session/session.py @@ -26,14 +26,16 @@ class User: :var id: User identifier. :var dc: Data-center identifier of the user's "home" DC. :var bot: :data:`True` if the user is from a bot account. + :var username: User's primary username. """ - __slots__ = ("id", "dc", "bot") + __slots__ = ("id", "dc", "bot", "username") - def __init__(self, *, id: int, dc: int, bot: bool) -> None: + def __init__(self, *, id: int, dc: int, bot: bool, username: Optional[str]) -> None: self.id = id self.dc = dc self.bot = bot + self.username = username class ChannelState: @@ -134,6 +136,7 @@ class Session: "id": self.user.id, "dc": self.user.dc, "bot": self.user.bot, + "username": self.user.username, } if self.user else None, @@ -174,6 +177,7 @@ class Session: id=dict["user"]["id"], dc=dict["user"]["dc"], bot=dict["user"]["bot"], + username=dict["user"]["username"], ) if dict["user"] else None, diff --git a/client/src/telethon/_impl/session/storage/sqlite.py b/client/src/telethon/_impl/session/storage/sqlite.py index 322ff9b3..f16e9097 100644 --- a/client/src/telethon/_impl/session/storage/sqlite.py +++ b/client/src/telethon/_impl/session/storage/sqlite.py @@ -106,7 +106,9 @@ class SqliteSession(Storage): DataCenter(id=id, addr=addr, auth=auth) for (id, addr, auth) in datacenter ], - user=User(id=user[0], dc=user[1], bot=bool(user[2])) if user else None, + user=User(id=user[0], dc=user[1], bot=bool(user[2]), username=user[3]) + if user + else None, state=UpdateState( pts=state[0], qts=state[1],