Handle PeerIdInvalidError in delete_dialog

This commit is contained in:
Lonami Exo 2020-01-07 12:13:42 +01:00
parent 582a61192a
commit d68d70362b
2 changed files with 19 additions and 5 deletions

View File

@ -3,7 +3,7 @@ import inspect
import itertools import itertools
import typing import typing
from .. import helpers, utils, hints from .. import helpers, utils, hints, errors
from ..requestiter import RequestIter from ..requestiter import RequestIter
from ..tl import types, functions, custom from ..tl import types, functions, custom
@ -435,14 +435,25 @@ class DialogMethods:
# Leaving a channel by username # Leaving a channel by username
await client.delete_dialog('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) entity = await self.get_input_entity(entity)
ty = helpers._entity_type(entity) ty = helpers._entity_type(entity)
if ty == helpers._EntityType.CHANNEL: if ty == helpers._EntityType.CHANNEL:
return await self(functions.channels.LeaveChannelRequest(entity)) return await self(functions.channels.LeaveChannelRequest(entity))
if ty == helpers._EntityType.CHAT: if ty == helpers._EntityType.CHAT and not deactivated:
try:
result = await self(functions.messages.DeleteChatUserRequest( result = await self(functions.messages.DeleteChatUserRequest(
entity.chat_id, types.InputUserSelf())) entity.chat_id, types.InputUserSelf()))
except errors.PeerIdInvalidError:
# Happens if we didn't have the deactivated information
result = None
else: else:
result = None result = None

View File

@ -112,7 +112,10 @@ class Dialog:
Shorthand for `telethon.client.dialogs.DialogMethods.delete_dialog` Shorthand for `telethon.client.dialogs.DialogMethods.delete_dialog`
with ``entity`` already set. 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): async def archive(self, folder=1):
""" """