From 59b43af02e79b80b1f3831e8157f2e128d61d263 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 28 Jun 2019 20:43:06 +0200 Subject: [PATCH 1/6] Fix RPCError not setting the x attribute --- pyrogram/errors/rpc_error.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyrogram/errors/rpc_error.py b/pyrogram/errors/rpc_error.py index fb3a717d..e0acdc21 100644 --- a/pyrogram/errors/rpc_error.py +++ b/pyrogram/errors/rpc_error.py @@ -40,6 +40,11 @@ class RPCError(Exception): 'caused by "{}"'.format(rpc_name) )) + try: + self.x = int(x) + except (ValueError, TypeError): + self.x = x + if is_unknown: with open("unknown_errors.txt", "a", encoding="utf-8") as f: f.write("{}\t{}\t{}\n".format(datetime.now(), x, rpc_name)) From 54c8e24f483ea905d97161a6994c83aadfd0ac43 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Fri, 28 Jun 2019 13:45:03 -0400 Subject: [PATCH 2/6] Add .set_ bound methods to Chat --- docs/source/api/bound-methods.rst | 6 ++ pyrogram/client/types/user_and_chats/chat.py | 107 +++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/docs/source/api/bound-methods.rst b/docs/source/api/bound-methods.rst index 83b3dbbe..d129ad16 100644 --- a/docs/source/api/bound-methods.rst +++ b/docs/source/api/bound-methods.rst @@ -67,6 +67,9 @@ Chat - :meth:`~Chat.archive` - :meth:`~Chat.unarchive` + - :meth:`~Chat.set_title` + - :meth:`~Chat.set_description` + - :meth:`~Chat.set_photo` User ^^^^ @@ -134,6 +137,9 @@ Details .. Chat .. automethod:: Chat.archive() .. automethod:: Chat.unarchive() +.. automethod:: Chat.set_title() +.. automethod:: Chat.set_description) +.. automethod:: Chat.set_photo() .. User .. automethod:: User.archive() diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 2d88d3ed..e99bccea 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -312,3 +312,110 @@ class Chat(Object): """ return self._client.unarchive_chats(self.id) + + def set_title(self, title: str) -> bool: + """Bound method *set_title* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.set_chat_title( + chat_id=chat_id, + title=title + ) + + Example: + .. code-block:: python + + chat.set_title("Lounge") + + Note: + In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" + setting is off. + + Parameters: + title (``str``): + New chat title, 1-255 characters. + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of Telegram RPC error. + ValueError: In case a chat_id belongs to user. + """ + + return self._client.set_chat_title( + chat_id=self.id, + title=title + ) + + def set_description(self, description: str) -> bool: + """Bound method *set_description* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.set_chat_description( + chat_id=chat_id, + description=description + ) + + Example: + .. code-block:: python + + chat.set_chat_description("Don't spam!") + + Parameters: + description (``str``): + New chat description, 0-255 characters. + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of Telegram RPC error. + ValueError If a chat_id doesn't belong to a supergroup or a channel. + """ + + return self._client.set_chat_description( + chat_id=self.id, + description=description + ) + + + def set_photo(self, photo: str) -> bool: + """Bound method *set_photo* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.set_chat_photo( + chat_id=chat_id, + photo=photo + ) + + Example: + .. code-block:: python + + chat.set_photo("photo.png") + + Parameters: + photo (``str``): + New chat photo. You can pass a :obj:`Photo` id or a file path to upload a new photo. + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of a Telegram RPC error. + ValueError: if a chat_id belongs to user. + """ + + return self._client.set_chat_photo( + chat_id=self.id, + photo=photo + ) From 2c1834b1b2191f066e26db38275632c294f39d18 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Fri, 28 Jun 2019 20:22:20 -0400 Subject: [PATCH 3/6] Add .(kick|unban|restrict|promote)_member bound methods to Chat --- docs/source/api/bound-methods.rst | 8 + pyrogram/client/types/user_and_chats/chat.py | 255 +++++++++++++++++++ 2 files changed, 263 insertions(+) diff --git a/docs/source/api/bound-methods.rst b/docs/source/api/bound-methods.rst index d129ad16..6a1cf1c3 100644 --- a/docs/source/api/bound-methods.rst +++ b/docs/source/api/bound-methods.rst @@ -70,6 +70,10 @@ Chat - :meth:`~Chat.set_title` - :meth:`~Chat.set_description` - :meth:`~Chat.set_photo` + - :meth:`~Chat.kick_member` + - :meth:`~Chat.unban_member` + - :meth:`~Chat.restrict_member` + - :meth:`~Chat.promote_member` User ^^^^ @@ -140,6 +144,10 @@ Details .. automethod:: Chat.set_title() .. automethod:: Chat.set_description) .. automethod:: Chat.set_photo() +.. automethod:: Chat.kick_member() +.. automethod:: Chat.unban_member() +.. automethod:: Chat.restrict_member() +.. automethod:: Chat.promote_member() .. User .. automethod:: User.archive() diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index e99bccea..556672d2 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -419,3 +419,258 @@ class Chat(Object): chat_id=self.id, photo=photo ) + + def kick_member( + self, + user_id: Union[int, str], + until_date: int = 0 + ) -> Union["pyrogram.Message", bool]: + """Bound method *kick_member* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.kick_chat_member( + chat_id=chat_id, + user_id=user_id + ) + + Example: + .. code-block:: python + + chat.kick_member(123456789) + + Note: + In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" setting is + off in the target group. Otherwise members may only be removed by the group's creator or by the member + that added them. + + Parameters: + user_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target user. + For a contact that exists in your Telegram address book you can use his phone number (str). + + until_date (``int``, *optional*): + Date when the user will be unbanned, unix time. + If user is banned for more than 366 days or less than 30 seconds from the current time they are + considered to be banned forever. Defaults to 0 (ban forever). + + Returns: + :obj:`Message` | ``bool``: On success, a service message will be returned (when applicable), otherwise, in + case a message object couldn't be returned, True is returned. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + + return self._client.kick_chat_member( + chat_id=self.id, + user_id=user_id, + until_date=until_date + ) + + def unban_member( + self, + user_id: Union[int, str] + ) -> bool: + """Bound method *unban_member* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.unban_chat_member( + chat_id=chat_id, + user_id=user_id + ) + + Example: + .. code-block:: python + + chat.unban_member(123456789) + + Parameters: + user_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target user. + For a contact that exists in your Telegram address book you can use his phone number (str). + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + + return self._client.unban_chat_member( + chat_id=self.id, + user_id=user_id, + ) + + def restrict_member( + self, + chat_id: Union[int, str], + user_id: Union[int, str], + until_date: int = 0, + can_send_messages: bool = False, + can_send_media_messages: bool = False, + can_send_other_messages: bool = False, + can_add_web_page_previews: bool = False, + can_send_polls: bool = False, + can_change_info: bool = False, + can_invite_users: bool = False, + can_pin_messages: bool = False + ) -> "pyrogram.Chat": + """Bound method *unban_member* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.restrict_chat_member( + chat_id=chat_id, + user_id=user_id + ) + + Example: + .. code-block:: python + + chat.restrict_member(123456789) + + Parameters: + user_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target user. + For a contact that exists in your Telegram address book you can use his phone number (str). + + until_date (``int``, *optional*): + Date when the user will be unbanned, unix time. + If user is banned for more than 366 days or less than 30 seconds from the current time they are + considered to be banned forever. Defaults to 0 (ban forever). + + can_send_messages (``bool``, *optional*): + Pass True, if the user can send text messages, contacts, locations and venues. + + can_send_media_messages (``bool``, *optional*): + Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes, + implies can_send_messages. + + can_send_other_messages (``bool``, *optional*): + Pass True, if the user can send animations, games, stickers and use inline bots, + implies can_send_media_messages. + + can_add_web_page_previews (``bool``, *optional*): + Pass True, if the user may add web page previews to their messages, implies can_send_media_messages. + + can_send_polls (``bool``, *optional*): + Pass True, if the user can send polls, implies can_send_media_messages. + + can_change_info (``bool``, *optional*): + Pass True, if the user can change the chat title, photo and other settings. + + can_invite_users (``bool``, *optional*): + Pass True, if the user can invite new users to the chat. + + can_pin_messages (``bool``, *optional*): + Pass True, if the user can pin messages. + + Returns: + :obj:`Chat`: On success, a chat object is returned. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + + return self._client.restrict_chat_member( + self, + chat_id=self.id, + user_id=user_id, + until_date=until_date, + can_send_messages=can_send_messages, + can_send_media_messages=can_send_media_messages, + can_send_other_messages=can_send_other_messages, + can_add_web_page_previews=can_add_web_page_previews, + can_send_polls=can_send_polls, + can_change_info=can_change_info, + can_invite_users=can_invite_users, + can_pin_messages=can_pin_messages + ) + + def promote_member( + chat_id: Union[int, str], + user_id: Union[int, str], + can_change_info: bool = True, + can_post_messages: bool = False, + can_edit_messages: bool = False, + can_delete_messages: bool = True, + can_restrict_members: bool = True, + can_invite_users: bool = True, + can_pin_messages: bool = False, + can_promote_members: bool = False + ) -> bool: + """Bound method *promote_member* of :obj:`Chat`. + + Use as a shortcut for: + + .. code-block:: python + + client.promote_chat_member( + chat_id=chat_id, + user_id=user_id + ) + + Example: + + .. code-block:: python + + chat.promote_member(123456789) + + Parameters: + user_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target user. + For a contact that exists in your Telegram address book you can use his phone number (str). + + can_change_info (``bool``, *optional*): + Pass True, if the administrator can change chat title, photo and other settings. + + can_post_messages (``bool``, *optional*): + Pass True, if the administrator can create channel posts, channels only. + + can_edit_messages (``bool``, *optional*): + Pass True, if the administrator can edit messages of other users and can pin messages, channels only. + + can_delete_messages (``bool``, *optional*): + Pass True, if the administrator can delete messages of other users. + + can_restrict_members (``bool``, *optional*): + Pass True, if the administrator can restrict, ban or unban chat members. + + can_invite_users (``bool``, *optional*): + Pass True, if the administrator can invite new users to the chat. + + can_pin_messages (``bool``, *optional*): + Pass True, if the administrator can pin messages, supergroups only. + + can_promote_members (``bool``, *optional*): + Pass True, if the administrator can add new administrators with a subset of his own privileges or + demote administrators that he has promoted, directly or indirectly (promoted by administrators that + were appointed by him). + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of a Telegram RPC error. + """ + + return self._client.promote_chat_member( + chat_id=self.id, + user_id=user_id, + can_change_info=can_change_info, + can_post_messages=can_post_messages, + can_edit_messages=can_edit_messages, + can_delete_messages=can_delete_messages, + can_restrict_members=can_restrict_members, + can_invite_users=can_invite_users, + can_pin_messages=can_pin_messages, + can_promote_members=can_promote_members + ) From a02cd271c94540944977eb304e90c5d6415e924b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 30 Jun 2019 10:01:39 +0200 Subject: [PATCH 4/6] Update chat.py --- pyrogram/client/types/user_and_chats/chat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 556672d2..0e53ae35 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -313,6 +313,7 @@ class Chat(Object): return self._client.unarchive_chats(self.id) + # TODO: Remove notes about "All Members Are Admins" for basic groups, the attribute doesn't exist anymore def set_title(self, title: str) -> bool: """Bound method *set_title* of :obj:`Chat`. From 80d7a8cbbd4fd1e4b04da8a5209e8e01cc78783b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 30 Jun 2019 10:03:45 +0200 Subject: [PATCH 5/6] Add missing colon --- pyrogram/client/types/user_and_chats/chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 0e53ae35..90205fbf 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -378,7 +378,7 @@ class Chat(Object): Raises: RPCError: In case of Telegram RPC error. - ValueError If a chat_id doesn't belong to a supergroup or a channel. + ValueError: If a chat_id doesn't belong to a supergroup or a channel. """ return self._client.set_chat_description( From 09e1ac5eb49b3024eef7c1a0bfaccae192aac3a1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 30 Jun 2019 10:48:19 +0200 Subject: [PATCH 6/6] Fix bad params when defining and calling methods --- pyrogram/client/types/user_and_chats/chat.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index 90205fbf..586b49fe 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -510,7 +510,6 @@ class Chat(Object): def restrict_member( self, - chat_id: Union[int, str], user_id: Union[int, str], until_date: int = 0, can_send_messages: bool = False, @@ -582,7 +581,6 @@ class Chat(Object): """ return self._client.restrict_chat_member( - self, chat_id=self.id, user_id=user_id, until_date=until_date, @@ -597,7 +595,7 @@ class Chat(Object): ) def promote_member( - chat_id: Union[int, str], + self, user_id: Union[int, str], can_change_info: bool = True, can_post_messages: bool = False,