From bfa995d52bf4cbb0c4b89db666732b4ff01932f6 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 5 Jun 2020 21:17:09 +0200 Subject: [PATCH] Don't crash when receiving updates prior to login Fixes #1467, and enables #1471. --- telethon/client/updates.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 10eb8a77..6f93351b 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -397,6 +397,9 @@ class UpdateMethods: # Some updates require our own ID, so we must make sure # that the event builder has offline access to it. Calling # `get_me()` will cache it under `self._self_input_peer`. + # + # It will return `None` if we haven't logged in yet which is + # fine, we will just retry next time anyway. await self.get_me(input_peer=True) built = EventBuilderDict(self, update, others) @@ -566,8 +569,15 @@ class EventBuilderDict: try: return self.__dict__[builder] except KeyError: + # Updates may arrive before login (like updateLoginToken) and we + # won't have our self ID yet (anyway only new messages need it). + self_id = ( + self.client._self_input_peer.user_id + if self.client._self_input_peer + else None + ) event = self.__dict__[builder] = builder.build( - self.update, self.others, self.client._self_input_peer.user_id) + self.update, self.others, self_id) if isinstance(event, EventCommon): event.original_update = self.update