mirror of
https://github.com/PaiGramTeam/PaiGram.git
synced 2024-12-27 11:49:04 +00:00
471ed052ea
使用 ChatMemberHandler 获取 chat member updates 解决在部分群开启了隐藏成员列表后Bot无法工作的问题
30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
from typing import Optional, Tuple
|
|
|
|
from telegram import ChatMemberUpdated, ChatMember
|
|
|
|
|
|
def extract_status_change(chat_member_update: ChatMemberUpdated) -> Optional[Tuple[bool, bool]]:
|
|
"""Takes a ChatMemberUpdated instance and extracts whether the 'old_chat_member' was a member
|
|
of the chat and whether the 'new_chat_member' is a member of the chat. Returns None, if
|
|
the status didn't change.
|
|
"""
|
|
status_change = chat_member_update.difference().get("status")
|
|
old_is_member, new_is_member = chat_member_update.difference().get("is_member", (None, None))
|
|
|
|
if status_change is None:
|
|
return None
|
|
|
|
old_status, new_status = status_change
|
|
was_member = old_status in [
|
|
ChatMember.MEMBER,
|
|
ChatMember.OWNER,
|
|
ChatMember.ADMINISTRATOR,
|
|
] or (old_status == ChatMember.RESTRICTED and old_is_member is True)
|
|
is_member = new_status in [
|
|
ChatMember.MEMBER,
|
|
ChatMember.OWNER,
|
|
ChatMember.ADMINISTRATOR,
|
|
] or (new_status == ChatMember.RESTRICTED and new_is_member is True)
|
|
|
|
return was_member, is_member
|