Return the service message produced when kicking somebody

Helps with #1664.
This commit is contained in:
Lonami Exo 2021-01-02 12:13:53 +01:00
parent acd4c8648e
commit d0ee3c3a56
2 changed files with 18 additions and 8 deletions

View File

@ -1135,11 +1135,16 @@ class ChatMethods:
user (`entity`, optional): user (`entity`, optional):
The user to kick. The user to kick.
Returns
Returns the service `Message <telethon.tl.custom.message.Message>`
produced about a user being kicked, if any.
Example Example
.. code-block:: python .. code-block:: python
# Kick some user from some chat # Kick some user from some chat, and deleting the service message
await client.kick_participant(chat, user) msg = await client.kick_participant(chat, user)
await msg.delete()
# Leaving chat # Leaving chat
await client.kick_participant(chat, 'me') await client.kick_participant(chat, 'me')
@ -1151,15 +1156,18 @@ class ChatMethods:
ty = helpers._entity_type(entity) ty = helpers._entity_type(entity)
if ty == helpers._EntityType.CHAT: if ty == helpers._EntityType.CHAT:
await self(functions.messages.DeleteChatUserRequest(entity.chat_id, user)) resp = await self(functions.messages.DeleteChatUserRequest(entity.chat_id, user))
elif ty == helpers._EntityType.CHANNEL: elif ty == helpers._EntityType.CHANNEL:
if isinstance(user, types.InputPeerSelf): if isinstance(user, types.InputPeerSelf):
await self(functions.channels.LeaveChannelRequest(entity)) # Despite no longer being in the channel, the account still
# seems to get the service message.
resp = await self(functions.channels.LeaveChannelRequest(entity))
else: else:
await self(functions.channels.EditBannedRequest( resp = await self(functions.channels.EditBannedRequest(
channel=entity, channel=entity,
user_id=user, user_id=user,
banned_rights=types.ChatBannedRights(until_date=None, view_messages=True) banned_rights=types.ChatBannedRights(
until_date=None, view_messages=True)
)) ))
await asyncio.sleep(0.5) await asyncio.sleep(0.5)
await self(functions.channels.EditBannedRequest( await self(functions.channels.EditBannedRequest(
@ -1170,6 +1178,8 @@ class ChatMethods:
else: else:
raise ValueError('You must pass either a channel or a chat') raise ValueError('You must pass either a channel or a chat')
return self._get_response_message(None, resp, entity)
async def get_permissions( async def get_permissions(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',

View File

@ -134,7 +134,7 @@ class MessageParseMethods:
# Pinning a message with `updatePinnedMessage` seems to # Pinning a message with `updatePinnedMessage` seems to
# always produce a service message we can't map so return # always produce a service message we can't map so return
# it directly. # it directly. The same happens for kicking users.
# #
# It could also be a list (e.g. when sending albums). # It could also be a list (e.g. when sending albums).
# #