From f03f5ffd794ecb6bf70ad5e97e38fe015a6f2651 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Sun, 21 Apr 2024 17:48:01 +0200 Subject: [PATCH] pyrofork: Update ChatMemberUpdatedHandler to support UpdateBotStopped updates Signed-off-by: wulan17 --- pyrogram/dispatcher.py | 4 +-- pyrogram/enums/chat_member_status.py | 4 +-- .../user_and_chats/chat_member_updated.py | 35 +++++++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/pyrogram/dispatcher.py b/pyrogram/dispatcher.py index 14714724..4931e34e 100644 --- a/pyrogram/dispatcher.py +++ b/pyrogram/dispatcher.py @@ -53,7 +53,7 @@ from pyrogram.raw.types import ( UpdateDeleteMessages, UpdateDeleteChannelMessages, UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery, UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll, - UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant, + UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant, UpdateBotStopped, UpdateBotChatInviteRequester, UpdateStory, UpdateBotMessageReaction, UpdateBotMessageReactions @@ -70,7 +70,7 @@ class Dispatcher: DELETE_MESSAGES_UPDATES = (UpdateDeleteMessages, UpdateDeleteChannelMessages) DELETE_BOT_BUSINESS_MESSAGES_UPDATES = (UpdateBotDeleteBusinessMessage,) CALLBACK_QUERY_UPDATES = (UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery) - CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant) + CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant, UpdateBotStopped,) USER_STATUS_UPDATES = (UpdateUserStatus,) BOT_INLINE_QUERY_UPDATES = (UpdateBotInlineQuery,) POLL_UPDATES = (UpdateMessagePoll,) diff --git a/pyrogram/enums/chat_member_status.py b/pyrogram/enums/chat_member_status.py index e70c1989..54c17547 100644 --- a/pyrogram/enums/chat_member_status.py +++ b/pyrogram/enums/chat_member_status.py @@ -25,7 +25,7 @@ from .auto_name import AutoName class ChatMemberStatus(AutoName): """Chat member status enumeration used in :obj:`~pyrogram.types.ChatMember`.""" - OWNER = auto() + OWNER = auto() # TODO: rename to 'creator' "Chat owner" ADMINISTRATOR = auto() @@ -40,5 +40,5 @@ class ChatMemberStatus(AutoName): LEFT = auto() "Left chat member" - BANNED = auto() + BANNED = auto() # TODO: rename to 'kicked' "Banned chat member" diff --git a/pyrogram/types/user_and_chats/chat_member_updated.py b/pyrogram/types/user_and_chats/chat_member_updated.py index 6b3e73ea..e695975f 100644 --- a/pyrogram/types/user_and_chats/chat_member_updated.py +++ b/pyrogram/types/user_and_chats/chat_member_updated.py @@ -21,8 +21,7 @@ from datetime import datetime from typing import Dict, Union import pyrogram -from pyrogram import raw, utils -from pyrogram import types +from pyrogram import enums, raw, types, utils from ..object import Object from ..update import Update @@ -78,10 +77,40 @@ class ChatMemberUpdated(Object, Update): @staticmethod def _parse( client: "pyrogram.Client", - update: Union["raw.types.UpdateChatParticipant", "raw.types.UpdateChannelParticipant"], + update: Union["raw.types.UpdateChatParticipant", "raw.types.UpdateChannelParticipant", "raw.types.UpdateBotStopped"], users: Dict[int, "raw.types.User"], chats: Dict[int, "raw.types.Chat"] ) -> "ChatMemberUpdated": + if isinstance(update, raw.types.UpdateBotStopped): + from_user = types.User._parse(client, users[update.user_id]) + _chat_member_one = types.ChatMember( + user=from_user, + status=enums.ChatMemberStatus.BANNED, + client=client + ) + _chat_member_two = types.ChatMember( + user=from_user, + status=enums.ChatMemberStatus.MEMBER, + client=client + ) + if update.stopped: + return ChatMemberUpdated( + chat=types.Chat._parse_chat(client, users[update.user_id]), + from_user=from_user, + date=utils.timestamp_to_datetime(update.date), + old_chat_member=_chat_member_two, + new_chat_member=_chat_member_one, + client=client + ) + return ChatMemberUpdated( + chat=types.Chat._parse_chat(client, users[update.user_id]), + from_user=from_user, + date=utils.timestamp_to_datetime(update.date), + old_chat_member=_chat_member_one, + new_chat_member=_chat_member_two, + client=client + ) + chat_id = getattr(update, "chat_id", None) or getattr(update, "channel_id") old_chat_member = None