Skip to content

Commit

Permalink
Update ChatMemberUpdatedHandler to support UpdateBotStopped updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Apr 21, 2024
1 parent 3441687 commit 84a9738
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ it can take advantage of new goodies!
| Scheme layer used: 177 |
+------------------------+

- Updated :obj:
- **BOTS ONLY**: Updated :obj:`~pyrogram.handlers.ChatMemberUpdatedHandler` to handle updates when the bot is blocked or unblocked by a user.
- Added missing parameters in :meth:`~pyrogram.Client.create_group`, :meth:`~pyrogram.Client.create_supergroup`, :meth:`~pyrogram.Client.create_channel`.
- Try to return the service message (when applicable) in the methods :meth:`~pyrogram.Client.add_chat_members`, :meth:`~pyrogram.Client.promote_chat_member`
- Add :obj:`~pyrogram.enums.ChatAction.TRIGGER_EMOJI_ANIMATION` and :obj:`~pyrogram.enums.ChatAction.WATCH_EMOJI_ANIMATION` in :meth:`~pyrogram.Client.send_chat_action` and :meth:`~pyrogram.types.Message.reply_chat_action`.
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
UpdateDeleteMessages, UpdateDeleteChannelMessages,
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant,
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant, UpdateBotStopped,
UpdateBotChatInviteRequester,
UpdateBotMessageReaction,
UpdateBotMessageReactions,
Expand All @@ -68,7 +68,7 @@ class Dispatcher:
EDIT_MESSAGE_UPDATES = (UpdateEditMessage, UpdateEditChannelMessage, UpdateBotEditBusinessMessage)
DELETE_MESSAGES_UPDATES = (UpdateDeleteMessages, UpdateDeleteChannelMessages, 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,)
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/enums/chat_member_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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()
Expand All @@ -39,5 +39,5 @@ class ChatMemberStatus(AutoName):
LEFT = auto()
"Left chat member"

BANNED = auto()
BANNED = auto() # TODO: rename to 'kicked'
"Banned chat member"
37 changes: 33 additions & 4 deletions pyrogram/types/user_and_chats/chat_member_updated.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
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

Expand Down Expand Up @@ -62,7 +61,7 @@ def __init__(
old_chat_member: "types.ChatMember",
new_chat_member: "types.ChatMember",
invite_link: "types.ChatInviteLink" = None,
via_chat_folder_invite_link: bool = False
via_chat_folder_invite_link: bool = None
):
super().__init__(client)

Expand All @@ -77,10 +76,40 @@ def __init__(
@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
Expand Down

0 comments on commit 84a9738

Please sign in to comment.