From d68d70362b0bb2f99162d290c33ede6ab4c19db1 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 7 Jan 2020 12:13:42 +0100 Subject: [PATCH] Handle PeerIdInvalidError in delete_dialog --- telethon/client/dialogs.py | 19 +++++++++++++++---- telethon/tl/custom/dialog.py | 5 ++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/telethon/client/dialogs.py b/telethon/client/dialogs.py index 4ddc726d..4bb74c27 100644 --- a/telethon/client/dialogs.py +++ b/telethon/client/dialogs.py @@ -3,7 +3,7 @@ import inspect import itertools import typing -from .. import helpers, utils, hints +from .. import helpers, utils, hints, errors from ..requestiter import RequestIter from ..tl import types, functions, custom @@ -435,14 +435,25 @@ class DialogMethods: # Leaving a channel by username await client.delete_dialog('username') """ + # If we have enough information (`Dialog.delete` gives it to us), + # then we know we don't have to kick ourselves in deactivated chats. + if isinstance(entity, types.Chat): + deactivated = entity.deactivated + else: + deactivated = False + entity = await self.get_input_entity(entity) ty = helpers._entity_type(entity) if ty == helpers._EntityType.CHANNEL: return await self(functions.channels.LeaveChannelRequest(entity)) - if ty == helpers._EntityType.CHAT: - result = await self(functions.messages.DeleteChatUserRequest( - entity.chat_id, types.InputUserSelf())) + if ty == helpers._EntityType.CHAT and not deactivated: + try: + result = await self(functions.messages.DeleteChatUserRequest( + entity.chat_id, types.InputUserSelf())) + except errors.PeerIdInvalidError: + # Happens if we didn't have the deactivated information + result = None else: result = None diff --git a/telethon/tl/custom/dialog.py b/telethon/tl/custom/dialog.py index b71b4c3f..79ef1131 100644 --- a/telethon/tl/custom/dialog.py +++ b/telethon/tl/custom/dialog.py @@ -112,7 +112,10 @@ class Dialog: Shorthand for `telethon.client.dialogs.DialogMethods.delete_dialog` with ``entity`` already set. """ - await self._client.delete_dialog(self.input_entity, revoke=revoke) + # Pass the entire entity so the method can determine whether + # the `Chat` is deactivated (in which case we don't kick ourselves, + # or it would raise `PEER_ID_INVALID`). + await self._client.delete_dialog(self.entity, revoke=revoke) async def archive(self, folder=1): """