Skip to content

Commit 18c524f

Browse files
committed
ChatMemberXXX classes redesigned
1 parent 6bc1c36 commit 18c524f

File tree

1 file changed

+102
-98
lines changed

1 file changed

+102
-98
lines changed

telebot/types.py

Lines changed: 102 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,7 +3251,7 @@ def __init__(self, small_file_id, small_file_unique_id, big_file_id, big_file_un
32513251
self.big_file_unique_id: str = big_file_unique_id
32523252

32533253

3254-
class ChatMember(JsonDeserializable):
3254+
class ChatMember(JsonDeserializable, ABC):
32553255
"""
32563256
This object contains information about one member of a chat.
32573257
Currently, the following 6 types of chat members are supported:
@@ -3266,78 +3266,31 @@ class ChatMember(JsonDeserializable):
32663266
Telegram Documentation: https://core.telegram.org/bots/api#chatmember
32673267
"""
32683268

3269+
def __init__(self, user, status, **kwargs):
3270+
self.user: User = user
3271+
self.status: str = status
3272+
32693273
@classmethod
32703274
def de_json(cls, json_string):
32713275
if json_string is None: return None
32723276
obj = cls.check_json(json_string)
32733277
obj['user'] = User.de_json(obj['user'])
3274-
member_type = obj['status']
3278+
status = obj['status']
32753279
# Ordered according to estimated appearance frequency.
3276-
if member_type == "member":
3280+
if status == "member":
32773281
return ChatMemberMember(**obj)
3278-
elif member_type == "left":
3282+
elif status == "left":
32793283
return ChatMemberLeft(**obj)
3280-
elif member_type == "kicked":
3284+
elif status == "kicked":
32813285
return ChatMemberBanned(**obj)
3282-
elif member_type == "restricted":
3286+
elif status == "restricted":
32833287
return ChatMemberRestricted(**obj)
3284-
elif member_type == "administrator":
3288+
elif status == "administrator":
32853289
return ChatMemberAdministrator(**obj)
3286-
elif member_type == "creator":
3290+
elif status == "creator":
32873291
return ChatMemberOwner(**obj)
32883292
else:
3289-
# Should not be here. For "if something happen" compatibility
3290-
return cls(**obj)
3291-
3292-
def __init__(self, user, status, custom_title=None, is_anonymous=None, can_be_edited=None,
3293-
can_post_messages=None, can_edit_messages=None, can_delete_messages=None,
3294-
can_restrict_members=None, can_promote_members=None, can_change_info=None,
3295-
can_invite_users=None, can_pin_messages=None, is_member=None,
3296-
can_send_messages=None, can_send_audios=None, can_send_documents=None,
3297-
can_send_photos=None, can_send_videos=None, can_send_video_notes=None,
3298-
can_send_voice_notes=None,
3299-
can_send_polls=None,
3300-
can_send_other_messages=None, can_add_web_page_previews=None,
3301-
can_manage_chat=None, can_manage_video_chats=None,
3302-
until_date=None, can_manage_topics=None,
3303-
can_post_stories=None, can_edit_stories=None, can_delete_stories=None,
3304-
**kwargs):
3305-
self.user: User = user
3306-
self.status: str = status
3307-
self.custom_title: str = custom_title
3308-
self.is_anonymous: bool = is_anonymous
3309-
self.can_be_edited: bool = can_be_edited
3310-
self.can_post_messages: bool = can_post_messages
3311-
self.can_edit_messages: bool = can_edit_messages
3312-
self.can_delete_messages: bool = can_delete_messages
3313-
self.can_restrict_members: bool = can_restrict_members
3314-
self.can_promote_members: bool = can_promote_members
3315-
self.can_change_info: bool = can_change_info
3316-
self.can_invite_users: bool = can_invite_users
3317-
self.can_pin_messages: bool = can_pin_messages
3318-
self.is_member: bool = is_member
3319-
self.can_send_messages: bool = can_send_messages
3320-
self.can_send_polls: bool = can_send_polls
3321-
self.can_send_other_messages: bool = can_send_other_messages
3322-
self.can_add_web_page_previews: bool = can_add_web_page_previews
3323-
self.can_manage_chat: bool = can_manage_chat
3324-
self.can_manage_video_chats: bool = can_manage_video_chats
3325-
self.until_date: int = until_date
3326-
self.can_manage_topics: bool = can_manage_topics
3327-
self.can_send_audios: bool = can_send_audios
3328-
self.can_send_documents: bool = can_send_documents
3329-
self.can_send_photos: bool = can_send_photos
3330-
self.can_send_videos: bool = can_send_videos
3331-
self.can_send_video_notes: bool = can_send_video_notes
3332-
self.can_send_voice_notes: bool = can_send_voice_notes
3333-
self.can_post_stories: bool = can_post_stories
3334-
self.can_edit_stories: bool = can_edit_stories
3335-
self.can_delete_stories: bool = can_delete_stories
3336-
3337-
@property
3338-
def can_manage_voice_chats(self):
3339-
log_deprecation_warning('The parameter "can_manage_voice_chats" is deprecated. Use "can_manage_video_chats" instead.')
3340-
return self.can_manage_video_chats
3293+
raise ValueError(f"Unknown chat member type: {status}.")
33413294

33423295

33433296
# noinspection PyUnresolvedReferences
@@ -3362,7 +3315,10 @@ class ChatMemberOwner(ChatMember):
33623315
:return: Instance of the class
33633316
:rtype: :class:`telebot.types.ChatMemberOwner`
33643317
"""
3365-
pass
3318+
def __init__(self, user, status, is_anonymous, custom_title=None, **kwargs):
3319+
super().__init__(user, status, **kwargs)
3320+
self.is_anonymous: bool = is_anonymous
3321+
self.custom_title: Optional[str] = custom_title
33663322

33673323

33683324
# noinspection PyUnresolvedReferences
@@ -3409,36 +3365,60 @@ class ChatMemberAdministrator(ChatMember):
34093365
:param can_invite_users: True, if the user is allowed to invite new users to the chat
34103366
:type can_invite_users: :obj:`bool`
34113367

3368+
:param can_post_stories: True, if the administrator can post channel stories
3369+
:type can_post_stories: :obj:`bool`
3370+
3371+
:param can_edit_stories: True, if the administrator can edit stories
3372+
:type can_edit_stories: :obj:`bool`
3373+
3374+
:param can_delete_stories: True, if the administrator can delete stories of other users
3375+
:type can_delete_stories: :obj:`bool`
3376+
34123377
:param can_post_messages: Optional. True, if the administrator can post in the channel; channels only
34133378
:type can_post_messages: :obj:`bool`
34143379

3415-
:param can_edit_messages: Optional. True, if the administrator can edit messages of other users and can pin
3416-
messages; channels only
3380+
:param can_edit_messages: Optional. True, if the administrator can edit messages of other users and can pin messages; channels only
34173381
:type can_edit_messages: :obj:`bool`
34183382

34193383
:param can_pin_messages: Optional. True, if the user is allowed to pin messages; groups and supergroups only
34203384
:type can_pin_messages: :obj:`bool`
34213385

3422-
:param can_manage_topics: Optional. True, if the user is allowed to create, rename, close, and reopen forum topics;
3423-
supergroups only
3386+
:param can_manage_topics: Optional. True, if the user is allowed to create, rename, close, and reopen forum topics; supergroups only
34243387
:type can_manage_topics: :obj:`bool`
34253388

34263389
:param custom_title: Optional. Custom title for this user
34273390
:type custom_title: :obj:`str`
34283391

3429-
:param can_post_stories: Optional. True, if the administrator can post channel stories
3430-
:type can_post_stories: :obj:`bool`
3431-
3432-
:param can_edit_stories: Optional. True, if the administrator can edit stories
3433-
:type can_edit_stories: :obj:`bool`
3434-
3435-
:param can_delete_stories: Optional. True, if the administrator can delete stories of other users
3436-
:type can_delete_stories: :obj:`bool`
3437-
34383392
:return: Instance of the class
34393393
:rtype: :class:`telebot.types.ChatMemberAdministrator`
34403394
"""
3441-
pass
3395+
def __init__(self, user, status, can_be_edited, is_anonymous, can_manage_chat, can_delete_messages,
3396+
can_manage_video_chats, can_restrict_members, can_promote_members, can_change_info, can_invite_users,
3397+
can_post_stories, can_edit_stories, can_delete_stories, can_post_messages=None, can_edit_messages=None,
3398+
can_pin_messages=None, can_manage_topics=None, custom_title=None, **kwargs):
3399+
super().__init__(user, status, **kwargs)
3400+
self.can_be_edited: bool = can_be_edited
3401+
self.is_anonymous: bool = is_anonymous
3402+
self.can_manage_chat: bool = can_manage_chat
3403+
self.can_delete_messages: bool = can_delete_messages
3404+
self.can_manage_video_chats: bool = can_manage_video_chats
3405+
self.can_restrict_members: bool = can_restrict_members
3406+
self.can_promote_members: bool = can_promote_members
3407+
self.can_change_info: bool = can_change_info
3408+
self.can_invite_users: bool = can_invite_users
3409+
self.can_post_stories: bool = can_post_stories
3410+
self.can_edit_stories: bool = can_edit_stories
3411+
self.can_delete_stories: bool = can_delete_stories
3412+
self.can_post_messages: Optional[bool] = can_post_messages
3413+
self.can_edit_messages: Optional[bool] = can_edit_messages
3414+
self.can_pin_messages: Optional[bool] = can_pin_messages
3415+
self.can_manage_topics: Optional[bool] = can_manage_topics
3416+
self.custom_title: Optional[str] = custom_title
3417+
3418+
@property
3419+
def can_manage_voice_chats(self):
3420+
log_deprecation_warning('The parameter "can_manage_voice_chats" is deprecated. Use "can_manage_video_chats" instead.')
3421+
return self.can_manage_video_chats
34423422

34433423

34443424
# noinspection PyUnresolvedReferences
@@ -3454,10 +3434,15 @@ class ChatMemberMember(ChatMember):
34543434
:param user: Information about the user
34553435
:type user: :class:`telebot.types.User`
34563436

3437+
:param until_date: Optional. Date when the user's subscription will expire; Unix time. If 0, then the user is a member forever
3438+
:type until_date: :obj:`int`
3439+
34573440
:return: Instance of the class
34583441
:rtype: :class:`telebot.types.ChatMemberMember`
34593442
"""
3460-
pass
3443+
def __init__(self, user, status, until_date=None, **kwargs):
3444+
super().__init__(user, status, **kwargs)
3445+
self.until_date: Optional[int] = until_date
34613446

34623447

34633448
# noinspection PyUnresolvedReferences
@@ -3476,18 +3461,6 @@ class ChatMemberRestricted(ChatMember):
34763461
:param is_member: True, if the user is a member of the chat at the moment of the request
34773462
:type is_member: :obj:`bool`
34783463

3479-
:param can_change_info: True, if the user is allowed to change the chat title, photo and other settings
3480-
:type can_change_info: :obj:`bool`
3481-
3482-
:param can_invite_users: True, if the user is allowed to invite new users to the chat
3483-
:type can_invite_users: :obj:`bool`
3484-
3485-
:param can_pin_messages: True, if the user is allowed to pin messages
3486-
:type can_pin_messages: :obj:`bool`
3487-
3488-
:param can_manage_topics: True, if the user is allowed to create forum topics
3489-
:type can_manage_topics: :obj:`bool`
3490-
34913464
:param can_send_messages: True, if the user is allowed to send text messages, contacts, locations and venues
34923465
:type can_send_messages: :obj:`bool`
34933466

@@ -3512,21 +3485,52 @@ class ChatMemberRestricted(ChatMember):
35123485
:param can_send_polls: True, if the user is allowed to send polls
35133486
:type can_send_polls: :obj:`bool`
35143487

3515-
:param can_send_other_messages: True, if the user is allowed to send animations, games, stickers and use inline
3516-
bots
3488+
:param can_send_other_messages: True, if the user is allowed to send animations, games, stickers and use inline bots
35173489
:type can_send_other_messages: :obj:`bool`
35183490

35193491
:param can_add_web_page_previews: True, if the user is allowed to add web page previews to their messages
35203492
:type can_add_web_page_previews: :obj:`bool`
35213493

3522-
:param until_date: Date when restrictions will be lifted for this user; unix time. If 0, then the user is restricted
3523-
forever
3494+
:param can_change_info: True, if the user is allowed to change the chat title, photo and other settings
3495+
:type can_change_info: :obj:`bool`
3496+
3497+
:param can_invite_users: True, if the user is allowed to invite new users to the chat
3498+
:type can_invite_users: :obj:`bool`
3499+
3500+
:param can_pin_messages: True, if the user is allowed to pin messages
3501+
:type can_pin_messages: :obj:`bool`
3502+
3503+
:param can_manage_topics: True, if the user is allowed to create forum topics
3504+
:type can_manage_topics: :obj:`bool`
3505+
3506+
:param until_date: Date when restrictions will be lifted for this user; unix time. If 0, then the user is restricted forever
35243507
:type until_date: :obj:`int`
35253508

35263509
:return: Instance of the class
35273510
:rtype: :class:`telebot.types.ChatMemberRestricted`
35283511
"""
3529-
pass
3512+
def __init__(self, user, status, is_member, can_send_messages, can_send_audios, can_send_documents,
3513+
can_send_photos, can_send_videos, can_send_video_notes, can_send_voice_notes, can_send_polls,
3514+
can_send_other_messages, can_add_web_page_previews,
3515+
can_change_info, can_invite_users, can_pin_messages, can_manage_topics,
3516+
until_date=None, **kwargs):
3517+
super().__init__(user, status, **kwargs)
3518+
self.is_member: bool = is_member
3519+
self.can_send_messages: bool = can_send_messages
3520+
self.can_send_audios: bool = can_send_audios
3521+
self.can_send_documents: bool = can_send_documents
3522+
self.can_send_photos: bool = can_send_photos
3523+
self.can_send_videos: bool = can_send_videos
3524+
self.can_send_video_notes: bool = can_send_video_notes
3525+
self.can_send_voice_notes: bool = can_send_voice_notes
3526+
self.can_send_polls: bool = can_send_polls
3527+
self.can_send_other_messages: bool = can_send_other_messages
3528+
self.can_add_web_page_previews: bool = can_add_web_page_previews
3529+
self.can_change_info: bool = can_change_info
3530+
self.can_invite_users: bool = can_invite_users
3531+
self.can_pin_messages: bool = can_pin_messages
3532+
self.can_manage_topics: bool = can_manage_topics
3533+
self.until_date: Optional[int] = until_date
35303534

35313535

35323536
# noinspection PyUnresolvedReferences
@@ -3561,14 +3565,15 @@ class ChatMemberBanned(ChatMember):
35613565
:param user: Information about the user
35623566
:type user: :class:`telebot.types.User`
35633567

3564-
:param until_date: Date when restrictions will be lifted for this user; unix time. If 0, then the user is banned
3565-
forever
3568+
:param until_date: Date when restrictions will be lifted for this user; unix time. If 0, then the user is banned forever
35663569
:type until_date: :obj:`int`
35673570

35683571
:return: Instance of the class
35693572
:rtype: :class:`telebot.types.ChatMemberBanned`
35703573
"""
3571-
pass
3574+
def __init__(self, user, status, until_date=None, **kwargs):
3575+
super().__init__(user, status, **kwargs)
3576+
self.until_date: Optional[int] = until_date
35723577

35733578

35743579
class ChatPermissions(JsonDeserializable, JsonSerializable, Dictionaryable):
@@ -3577,8 +3582,7 @@ class ChatPermissions(JsonDeserializable, JsonSerializable, Dictionaryable):
35773582

35783583
Telegram Documentation: https://core.telegram.org/bots/api#chatpermissions
35793584

3580-
:param can_send_messages: Optional. True, if the user is allowed to send text messages, contacts, locations and
3581-
venues
3585+
:param can_send_messages: Optional. True, if the user is allowed to send text messages, contacts, locations and venues
35823586
:type can_send_messages: :obj:`bool`
35833587

35843588
:param can_send_audios: Optional. True, if the user is allowed to send audios

0 commit comments

Comments
 (0)