Create new client.delete_dialog method

This commit is contained in:
Lonami Exo 2019-05-30 13:58:05 +02:00
parent e4158acd08
commit 0d64fd98f7
2 changed files with 54 additions and 10 deletions

View File

@ -323,6 +323,55 @@ class DialogMethods(UserMethods):
for x, y in zip(entities, folder)
]))
async def delete_dialog(
self: 'TelegramClient',
entity: 'hints.EntityLike',
*,
revoke: bool = False
):
"""
Deletes a dialog (leaves a chat or channel).
See also `Dialog.delete() <telethon.tl.custom.dialog.Dialog.delete>`.
Arguments
entity (entities):
The entity of the dialog to delete. If it's a chat or
channel, you will leave it. Note that the chat itself
is not deleted, only the dialog, because you left it.
revoke (`bool`, optional):
On private chats, you may revoke the messages from
the other peer too. By default, it's ``False``. Set
it to ``True`` to delete the history for both.
Returns
The :tl:`Updates` object that the request produces,
or nothing for private conversations.
Example
.. code-block:: python
# Deleting the first dialog
dialogs = client.get_dialogs(5)
client.delete_dialog(dialogs[0])
# Leaving a channel by username
client.delete_dialog('username')
"""
entity = await self.get_input_entity(entity)
if isinstance(entity, types.InputPeerChannel):
return await self(functions.channels.LeaveChannelRequest(entity))
if isinstance(entity, types.InputPeerChat):
result = await self(functions.messages.DeleteChatUserRequest(
entity.chat_id, types.InputUserSelf()))
else:
result = None
await self(functions.messages.DeleteHistoryRequest(entity, 0, revoke=revoke))
return result
def conversation(
self: 'TelegramClient',
entity: 'hints.EntityLike',

View File

@ -104,20 +104,15 @@ class Dialog:
return await self._client.send_message(
self.input_entity, *args, **kwargs)
async def delete(self):
async def delete(self, revoke=False):
"""
Deletes the dialog from your dialog list. If you own the
channel this won't destroy it, only delete it from the list.
Shorthand for `telethon.client.dialogs.DialogMethods.delete_dialog`
with ``entity`` already set.
"""
if self.is_channel:
await self._client(functions.channels.LeaveChannelRequest(
self.input_entity))
else:
if self.is_group:
await self._client(functions.messages.DeleteChatUserRequest(
self.entity.id, types.InputPeerSelf()))
await self._client(functions.messages.DeleteHistoryRequest(
self.input_entity, 0))
await self._client.delete_dialog(self.input_entity, revoke=revoke)
async def archive(self, folder=1):
"""