Skip to content

Commit 2c1834b

Browse files
committed
Add .(kick|unban|restrict|promote)_member bound methods to Chat
1 parent 54c8e24 commit 2c1834b

File tree

2 files changed

+263
-0
lines changed

2 files changed

+263
-0
lines changed

docs/source/api/bound-methods.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Chat
7070
- :meth:`~Chat.set_title`
7171
- :meth:`~Chat.set_description`
7272
- :meth:`~Chat.set_photo`
73+
- :meth:`~Chat.kick_member`
74+
- :meth:`~Chat.unban_member`
75+
- :meth:`~Chat.restrict_member`
76+
- :meth:`~Chat.promote_member`
7377

7478
User
7579
^^^^
@@ -140,6 +144,10 @@ Details
140144
.. automethod:: Chat.set_title()
141145
.. automethod:: Chat.set_description)
142146
.. automethod:: Chat.set_photo()
147+
.. automethod:: Chat.kick_member()
148+
.. automethod:: Chat.unban_member()
149+
.. automethod:: Chat.restrict_member()
150+
.. automethod:: Chat.promote_member()
143151

144152
.. User
145153
.. automethod:: User.archive()

pyrogram/client/types/user_and_chats/chat.py

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,258 @@ def set_photo(self, photo: str) -> bool:
419419
chat_id=self.id,
420420
photo=photo
421421
)
422+
423+
def kick_member(
424+
self,
425+
user_id: Union[int, str],
426+
until_date: int = 0
427+
) -> Union["pyrogram.Message", bool]:
428+
"""Bound method *kick_member* of :obj:`Chat`.
429+
430+
Use as a shortcut for:
431+
432+
.. code-block:: python
433+
434+
client.kick_chat_member(
435+
chat_id=chat_id,
436+
user_id=user_id
437+
)
438+
439+
Example:
440+
.. code-block:: python
441+
442+
chat.kick_member(123456789)
443+
444+
Note:
445+
In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" setting is
446+
off in the target group. Otherwise members may only be removed by the group's creator or by the member
447+
that added them.
448+
449+
Parameters:
450+
user_id (``int`` | ``str``):
451+
Unique identifier (int) or username (str) of the target user.
452+
For a contact that exists in your Telegram address book you can use his phone number (str).
453+
454+
until_date (``int``, *optional*):
455+
Date when the user will be unbanned, unix time.
456+
If user is banned for more than 366 days or less than 30 seconds from the current time they are
457+
considered to be banned forever. Defaults to 0 (ban forever).
458+
459+
Returns:
460+
:obj:`Message` | ``bool``: On success, a service message will be returned (when applicable), otherwise, in
461+
case a message object couldn't be returned, True is returned.
462+
463+
Raises:
464+
RPCError: In case of a Telegram RPC error.
465+
"""
466+
467+
return self._client.kick_chat_member(
468+
chat_id=self.id,
469+
user_id=user_id,
470+
until_date=until_date
471+
)
472+
473+
def unban_member(
474+
self,
475+
user_id: Union[int, str]
476+
) -> bool:
477+
"""Bound method *unban_member* of :obj:`Chat`.
478+
479+
Use as a shortcut for:
480+
481+
.. code-block:: python
482+
483+
client.unban_chat_member(
484+
chat_id=chat_id,
485+
user_id=user_id
486+
)
487+
488+
Example:
489+
.. code-block:: python
490+
491+
chat.unban_member(123456789)
492+
493+
Parameters:
494+
user_id (``int`` | ``str``):
495+
Unique identifier (int) or username (str) of the target user.
496+
For a contact that exists in your Telegram address book you can use his phone number (str).
497+
498+
Returns:
499+
``bool``: True on success.
500+
501+
Raises:
502+
RPCError: In case of a Telegram RPC error.
503+
"""
504+
505+
return self._client.unban_chat_member(
506+
chat_id=self.id,
507+
user_id=user_id,
508+
)
509+
510+
def restrict_member(
511+
self,
512+
chat_id: Union[int, str],
513+
user_id: Union[int, str],
514+
until_date: int = 0,
515+
can_send_messages: bool = False,
516+
can_send_media_messages: bool = False,
517+
can_send_other_messages: bool = False,
518+
can_add_web_page_previews: bool = False,
519+
can_send_polls: bool = False,
520+
can_change_info: bool = False,
521+
can_invite_users: bool = False,
522+
can_pin_messages: bool = False
523+
) -> "pyrogram.Chat":
524+
"""Bound method *unban_member* of :obj:`Chat`.
525+
526+
Use as a shortcut for:
527+
528+
.. code-block:: python
529+
530+
client.restrict_chat_member(
531+
chat_id=chat_id,
532+
user_id=user_id
533+
)
534+
535+
Example:
536+
.. code-block:: python
537+
538+
chat.restrict_member(123456789)
539+
540+
Parameters:
541+
user_id (``int`` | ``str``):
542+
Unique identifier (int) or username (str) of the target user.
543+
For a contact that exists in your Telegram address book you can use his phone number (str).
544+
545+
until_date (``int``, *optional*):
546+
Date when the user will be unbanned, unix time.
547+
If user is banned for more than 366 days or less than 30 seconds from the current time they are
548+
considered to be banned forever. Defaults to 0 (ban forever).
549+
550+
can_send_messages (``bool``, *optional*):
551+
Pass True, if the user can send text messages, contacts, locations and venues.
552+
553+
can_send_media_messages (``bool``, *optional*):
554+
Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
555+
implies can_send_messages.
556+
557+
can_send_other_messages (``bool``, *optional*):
558+
Pass True, if the user can send animations, games, stickers and use inline bots,
559+
implies can_send_media_messages.
560+
561+
can_add_web_page_previews (``bool``, *optional*):
562+
Pass True, if the user may add web page previews to their messages, implies can_send_media_messages.
563+
564+
can_send_polls (``bool``, *optional*):
565+
Pass True, if the user can send polls, implies can_send_media_messages.
566+
567+
can_change_info (``bool``, *optional*):
568+
Pass True, if the user can change the chat title, photo and other settings.
569+
570+
can_invite_users (``bool``, *optional*):
571+
Pass True, if the user can invite new users to the chat.
572+
573+
can_pin_messages (``bool``, *optional*):
574+
Pass True, if the user can pin messages.
575+
576+
Returns:
577+
:obj:`Chat`: On success, a chat object is returned.
578+
579+
Raises:
580+
RPCError: In case of a Telegram RPC error.
581+
"""
582+
583+
return self._client.restrict_chat_member(
584+
self,
585+
chat_id=self.id,
586+
user_id=user_id,
587+
until_date=until_date,
588+
can_send_messages=can_send_messages,
589+
can_send_media_messages=can_send_media_messages,
590+
can_send_other_messages=can_send_other_messages,
591+
can_add_web_page_previews=can_add_web_page_previews,
592+
can_send_polls=can_send_polls,
593+
can_change_info=can_change_info,
594+
can_invite_users=can_invite_users,
595+
can_pin_messages=can_pin_messages
596+
)
597+
598+
def promote_member(
599+
chat_id: Union[int, str],
600+
user_id: Union[int, str],
601+
can_change_info: bool = True,
602+
can_post_messages: bool = False,
603+
can_edit_messages: bool = False,
604+
can_delete_messages: bool = True,
605+
can_restrict_members: bool = True,
606+
can_invite_users: bool = True,
607+
can_pin_messages: bool = False,
608+
can_promote_members: bool = False
609+
) -> bool:
610+
"""Bound method *promote_member* of :obj:`Chat`.
611+
612+
Use as a shortcut for:
613+
614+
.. code-block:: python
615+
616+
client.promote_chat_member(
617+
chat_id=chat_id,
618+
user_id=user_id
619+
)
620+
621+
Example:
622+
623+
.. code-block:: python
624+
625+
chat.promote_member(123456789)
626+
627+
Parameters:
628+
user_id (``int`` | ``str``):
629+
Unique identifier (int) or username (str) of the target user.
630+
For a contact that exists in your Telegram address book you can use his phone number (str).
631+
632+
can_change_info (``bool``, *optional*):
633+
Pass True, if the administrator can change chat title, photo and other settings.
634+
635+
can_post_messages (``bool``, *optional*):
636+
Pass True, if the administrator can create channel posts, channels only.
637+
638+
can_edit_messages (``bool``, *optional*):
639+
Pass True, if the administrator can edit messages of other users and can pin messages, channels only.
640+
641+
can_delete_messages (``bool``, *optional*):
642+
Pass True, if the administrator can delete messages of other users.
643+
644+
can_restrict_members (``bool``, *optional*):
645+
Pass True, if the administrator can restrict, ban or unban chat members.
646+
647+
can_invite_users (``bool``, *optional*):
648+
Pass True, if the administrator can invite new users to the chat.
649+
650+
can_pin_messages (``bool``, *optional*):
651+
Pass True, if the administrator can pin messages, supergroups only.
652+
653+
can_promote_members (``bool``, *optional*):
654+
Pass True, if the administrator can add new administrators with a subset of his own privileges or
655+
demote administrators that he has promoted, directly or indirectly (promoted by administrators that
656+
were appointed by him).
657+
658+
Returns:
659+
``bool``: True on success.
660+
661+
Raises:
662+
RPCError: In case of a Telegram RPC error.
663+
"""
664+
665+
return self._client.promote_chat_member(
666+
chat_id=self.id,
667+
user_id=user_id,
668+
can_change_info=can_change_info,
669+
can_post_messages=can_post_messages,
670+
can_edit_messages=can_edit_messages,
671+
can_delete_messages=can_delete_messages,
672+
can_restrict_members=can_restrict_members,
673+
can_invite_users=can_invite_users,
674+
can_pin_messages=can_pin_messages,
675+
can_promote_members=can_promote_members
676+
)

0 commit comments

Comments
 (0)