1717# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818
1919from datetime import datetime
20- from typing import Union
20+ from typing import Optional , Union
2121
2222import pyrogram
2323from pyrogram import raw , types , utils
@@ -29,6 +29,7 @@ async def ban_chat_member(
2929 chat_id : Union [int , str ],
3030 user_id : Union [int , str ],
3131 until_date : datetime = utils .zero_datetime (),
32+ revoke_messages : Optional [bool ] = None ,
3233 ) -> Union ["types.Message" , bool ]:
3334 """Ban a user from a group, a supergroup or a channel.
3435 In the case of supergroups and channels, the user will not be able to return to the group on their own using
@@ -50,6 +51,9 @@ async def ban_chat_member(
5051 If user is banned for more than 366 days or less than 30 seconds from the current time they are
5152 considered to be banned forever. Defaults to epoch (ban forever).
5253
54+ revoke_messages (``bool``, *optional*):
55+ Pass True to delete all messages in the chat for the user who is being removed.
56+
5357 Returns:
5458 :obj:`~pyrogram.types.Message` | ``bool``: On success, a service message will be returned (when applicable),
5559 otherwise, in case a message object couldn't be returned, True is returned.
@@ -68,6 +72,9 @@ async def ban_chat_member(
6872 chat_peer = await self .resolve_peer (chat_id )
6973 user_peer = await self .resolve_peer (user_id )
7074
75+ if isinstance (chat_peer , (raw .types .InputPeerSelf , raw .types .InputPeerUser )):
76+ raise ValueError ("Can't ban members in private chats" )
77+
7178 if isinstance (chat_peer , raw .types .InputPeerChannel ):
7279 r = await self .invoke (
7380 raw .functions .channels .EditBanned (
@@ -86,13 +93,21 @@ async def ban_chat_member(
8693 ),
8794 )
8895 )
96+
97+ if revoke_messages :
98+ await self .invoke (
99+ raw .functions .channels .DeleteParticipantHistory (
100+ channel = chat_peer , participant = user_peer
101+ )
102+ )
89103 else :
104+ if not isinstance (user_peer , raw .types .InputPeerUser ):
105+ raise ValueError ("Can't ban chats in basic groups" )
106+
90107 r = await self .invoke (
91108 raw .functions .messages .DeleteChatUser (
92- chat_id = abs (chat_id ), user_id = user_peer
109+ chat_id = abs (chat_id ), user_id = user_peer , revoke_history = revoke_messages
93110 )
94111 )
95112
96- messages = await utils .parse_messages (client = self , messages = r )
97-
98- return messages [0 ] if messages else True
113+ return next (iter (await utils .parse_messages (client = self , messages = r )), True )
0 commit comments