Skip to content

Commit 9911b95

Browse files
committed
Merge remote-tracking branch 'KurimuzonAkuma/dev' into dev
2 parents 9544efb + 19cbe1b commit 9911b95

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

pyrogram/methods/chats/ban_chat_member.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from datetime import datetime
20-
from typing import Union
20+
from typing import Optional, Union
2121

2222
import pyrogram
2323
from 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)

pyrogram/types/messages_and_media/story.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,25 @@ async def _parse(
350350
] or None
351351
reactions_count = getattr(story.views, "reactions_count", None)
352352

353-
if isinstance(story.media, raw.types.MessageMediaPhoto):
354-
photo = types.Photo._parse(client, story.media.photo, story.media.ttl_seconds)
355-
media_type = enums.MessageMediaType.PHOTO
356-
else:
357-
doc = story.media.document
358-
attributes = {type(i): i for i in doc.attributes}
359-
video_attributes = attributes.get(raw.types.DocumentAttributeVideo, None)
360-
video = types.Video._parse(client, doc, video_attributes, alternative_videos=getattr(story.media, "alt_documents", []))
361-
media_type = enums.MessageMediaType.VIDEO
353+
photo = None
354+
video = None
355+
356+
media = story.media
357+
media_type = None
358+
359+
if media:
360+
if isinstance(media, raw.types.MessageMediaPhoto):
361+
photo = types.Photo._parse(client, media.photo, media.ttl_seconds)
362+
media_type = enums.MessageMediaType.PHOTO
363+
elif isinstance(media, raw.types.MessageMediaDocument):
364+
doc = media.document
365+
attributes = {type(i): i for i in doc.attributes}
366+
video_attributes = attributes.get(raw.types.DocumentAttributeVideo, None)
367+
video = types.Video._parse(client, doc, video_attributes, alternative_videos=getattr(story.media, "alt_documents", []))
368+
media_type = enums.MessageMediaType.VIDEO
369+
else:
370+
media_type = enums.MessageMediaType.UNSUPPORTED
371+
media = None
362372

363373
privacy_map = {
364374
raw.types.PrivacyValueAllowAll: enums.StoriesPrivacyRules.PUBLIC,

pyrogram/types/user_and_chats/chat.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,10 @@ async def set_ttl(self, ttl_seconds: int) -> "types.Message":
14961496
)
14971497

14981498
async def ban_member(
1499-
self, user_id: Union[int, str], until_date: datetime = utils.zero_datetime()
1499+
self,
1500+
user_id: Union[int, str],
1501+
until_date: datetime = utils.zero_datetime(),
1502+
revoke_messages: Optional[bool] = None
15001503
) -> Union["types.Message", bool]:
15011504
"""Bound method *ban_member* of :obj:`~pyrogram.types.Chat`.
15021505
@@ -1524,6 +1527,9 @@ async def ban_member(
15241527
If user is banned for more than 366 days or less than 30 seconds from the current time they are
15251528
considered to be banned forever. Defaults to epoch (ban forever).
15261529
1530+
revoke_messages (``bool``, *optional*):
1531+
Pass True to delete all messages in the chat for the user who is being removed.
1532+
15271533
Returns:
15281534
:obj:`~pyrogram.types.Message` | ``bool``: On success, a service message will be returned (when applicable), otherwise, in
15291535
case a message object couldn't be returned, True is returned.
@@ -1532,7 +1538,10 @@ async def ban_member(
15321538
RPCError: In case of a Telegram RPC error.
15331539
"""
15341540
return await self._client.ban_chat_member(
1535-
chat_id=self.id, user_id=user_id, until_date=until_date
1541+
chat_id=self.id,
1542+
user_id=user_id,
1543+
until_date=until_date,
1544+
revoke_messages=revoke_messages
15361545
)
15371546

15381547
async def unban_member(

0 commit comments

Comments
 (0)