Skip to content

Commit c6f346f

Browse files
committed
[Bot API 4.4] Update chat permissions
- Move can_* permissions back to ChatMember objects - Rename restrict_chat to set_chat_permissions - Update restrict_chat_member to accept a single ChatPermissions arg. - Update ChatPermissions to be the same as the one on the Bot API
1 parent 091552e commit c6f346f

7 files changed

Lines changed: 231 additions & 248 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def get_title_list(s: str) -> list:
189189
delete_chat_photo
190190
set_chat_title
191191
set_chat_description
192+
set_chat_permissions
192193
pin_chat_message
193194
unpin_chat_message
194195
get_chat
@@ -199,7 +200,6 @@ def get_title_list(s: str) -> list:
199200
get_dialogs
200201
iter_dialogs
201202
get_dialogs_count
202-
restrict_chat
203203
update_chat_username
204204
archive_chats
205205
unarchive_chats

pyrogram/client/methods/chats/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
from .leave_chat import LeaveChat
3939
from .pin_chat_message import PinChatMessage
4040
from .promote_chat_member import PromoteChatMember
41-
from .restrict_chat import RestrictChat
4241
from .restrict_chat_member import RestrictChatMember
4342
from .set_chat_description import SetChatDescription
43+
from .set_chat_permissions import SetChatPermissions
4444
from .set_chat_photo import SetChatPhoto
4545
from .set_chat_title import SetChatTitle
4646
from .unarchive_chats import UnarchiveChats
@@ -71,7 +71,7 @@ class Chats(
7171
IterDialogs,
7272
IterChatMembers,
7373
UpdateChatUsername,
74-
RestrictChat,
74+
SetChatPermissions,
7575
GetDialogsCount,
7676
ArchiveChats,
7777
UnarchiveChats,

pyrogram/client/methods/chats/restrict_chat_member.py

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,21 @@
2020

2121
from pyrogram.api import functions, types
2222
from ...ext import BaseClient
23-
from ...types.user_and_chats import Chat
23+
from ...types.user_and_chats import Chat, ChatPermissions
2424

2525

2626
class RestrictChatMember(BaseClient):
2727
def restrict_chat_member(
2828
self,
2929
chat_id: Union[int, str],
3030
user_id: Union[int, str],
31-
until_date: int = 0,
32-
can_send_messages: bool = False,
33-
can_send_media_messages: bool = False,
34-
can_send_other_messages: bool = False,
35-
can_add_web_page_previews: bool = False,
36-
can_send_polls: bool = False,
37-
can_change_info: bool = False,
38-
can_invite_users: bool = False,
39-
can_pin_messages: bool = False
31+
permissions: ChatPermissions,
32+
until_date: int = 0
4033
) -> Chat:
4134
"""Restrict a user in a supergroup.
4235
43-
The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
44-
Pass True for all boolean parameters to lift restrictions from a user.
36+
You must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
37+
Pass True for all permissions to lift restrictions from a user.
4538
4639
Parameters:
4740
chat_id (``int`` | ``str``):
@@ -51,37 +44,14 @@ def restrict_chat_member(
5144
Unique identifier (int) or username (str) of the target user.
5245
For a contact that exists in your Telegram address book you can use his phone number (str).
5346
47+
permissions (:obj:`ChatPermissions`):
48+
New user permissions.
49+
5450
until_date (``int``, *optional*):
5551
Date when the user will be unbanned, unix time.
5652
If user is banned for more than 366 days or less than 30 seconds from the current time they are
5753
considered to be banned forever. Defaults to 0 (ban forever).
5854
59-
can_send_messages (``bool``, *optional*):
60-
Pass True, if the user can send text messages, contacts, locations and venues.
61-
62-
can_send_media_messages (``bool``, *optional*):
63-
Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
64-
implies can_send_messages.
65-
66-
can_send_other_messages (``bool``, *optional*):
67-
Pass True, if the user can send animations, games, stickers and use inline bots,
68-
implies can_send_messages.
69-
70-
can_add_web_page_previews (``bool``, *optional*):
71-
Pass True, if the user may add web page previews to their messages, implies can_send_messages.
72-
73-
can_send_polls (``bool``, *optional*):
74-
Pass True, if the user can send polls, implies can_send_messages.
75-
76-
can_change_info (``bool``, *optional*):
77-
Pass True, if the user can change the chat title, photo and other settings.
78-
79-
can_invite_users (``bool``, *optional*):
80-
Pass True, if the user can invite new users to the chat.
81-
82-
can_pin_messages (``bool``, *optional*):
83-
Pass True, if the user can pin messages.
84-
8555
Returns:
8656
:obj:`Chat`: On success, a chat object is returned.
8757
@@ -90,14 +60,16 @@ def restrict_chat_member(
9060
9161
from time import time
9262
93-
# Completely restrict chat member forever
94-
app.restrict_chat_member(chat_id, user_id)
63+
from pyrogram import ChatPermissions
64+
65+
# Completely restrict chat member (mute) forever
66+
app.restrict_chat_member(chat_id, user_id, ChatPermissions())
9567
96-
# Chat member can't send messages for 24h
97-
app.restrict_chat_member(chat_id, user_id, int(time.time() + 86400))
68+
# Chat member muted for 24h
69+
app.restrict_chat_member(chat_id, user_id, ChatPermissions(), int(time.time() + 86400))
9870
9971
# Chat member can only send text messages
100-
app.restrict_chat_member(chat_id, user_id, can_send_messages=True)
72+
app.restrict_chat_member(chat_id, user_id, ChatPermissions(can_send_messages=True))
10173
"""
10274
send_messages = True
10375
send_media = True
@@ -111,35 +83,35 @@ def restrict_chat_member(
11183
invite_users = True
11284
pin_messages = True
11385

114-
if can_send_messages:
86+
if permissions.can_send_messages:
11587
send_messages = None
11688

117-
if can_send_media_messages:
89+
if permissions.can_send_media_messages:
11890
send_messages = None
11991
send_media = None
12092

121-
if can_send_other_messages:
93+
if permissions.can_send_other_messages:
12294
send_messages = None
12395
send_stickers = None
12496
send_gifs = None
12597
send_games = None
12698
send_inline = None
12799

128-
if can_add_web_page_previews:
100+
if permissions.can_add_web_page_previews:
129101
send_messages = None
130102
embed_links = None
131103

132-
if can_send_polls:
104+
if permissions.can_send_polls:
133105
send_messages = None
134106
send_polls = None
135107

136-
if can_change_info:
108+
if permissions.can_change_info:
137109
change_info = None
138110

139-
if can_invite_users:
111+
if permissions.can_invite_users:
140112
invite_users = None
141113

142-
if can_pin_messages:
114+
if permissions.can_pin_messages:
143115
pin_messages = None
144116

145117
r = self.send(

pyrogram/client/methods/chats/restrict_chat.py renamed to pyrogram/client/methods/chats/set_chat_permissions.py

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,47 @@
2020

2121
from pyrogram.api import functions, types
2222
from ...ext import BaseClient
23-
from ...types.user_and_chats import Chat
23+
from ...types.user_and_chats import Chat, ChatPermissions
2424

2525

26-
class RestrictChat(BaseClient):
27-
def restrict_chat(
26+
class SetChatPermissions(BaseClient):
27+
def set_chat_permissions(
2828
self,
2929
chat_id: Union[int, str],
30-
can_send_messages: bool = False,
31-
can_send_media_messages: bool = False,
32-
can_send_other_messages: bool = False,
33-
can_add_web_page_previews: bool = False,
34-
can_send_polls: bool = False,
35-
can_change_info: bool = False,
36-
can_invite_users: bool = False,
37-
can_pin_messages: bool = False
30+
permissions: ChatPermissions,
3831
) -> Chat:
39-
"""Restrict a chat.
40-
Pass True for all boolean parameters to lift restrictions from a chat.
32+
"""Set default chat permissions for all members.
33+
34+
You must be an administrator in the group or a supergroup for this to work and must have the
35+
*can_restrict_members* admin rights.
4136
4237
Parameters:
4338
chat_id (``int`` | ``str``):
4439
Unique identifier (int) or username (str) of the target chat.
4540
46-
can_send_messages (``bool``, *optional*):
47-
Pass True, if the user can send text messages, contacts, locations and venues.
48-
49-
can_send_media_messages (``bool``, *optional*):
50-
Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
51-
implies can_send_messages.
52-
53-
can_send_other_messages (``bool``, *optional*):
54-
Pass True, if the user can send animations, games, stickers and use inline bots,
55-
implies can_send_messages.
56-
57-
can_add_web_page_previews (``bool``, *optional*):
58-
Pass True, if the user may add web page previews to their messages, implies can_send_messages.
59-
60-
can_send_polls (``bool``, *optional*):
61-
Pass True, if the user can send polls, implies can_send_messages.
62-
63-
can_change_info (``bool``, *optional*):
64-
Pass True, if the user can change the chat title, photo and other settings.
65-
66-
can_invite_users (``bool``, *optional*):
67-
Pass True, if the user can invite new users to the chat.
68-
69-
can_pin_messages (``bool``, *optional*):
70-
Pass True, if the user can pin messages.
41+
permissions (:obj:`ChatPermissions`):
42+
New default chat permissions.
7143
7244
Returns:
7345
:obj:`Chat`: On success, a chat object is returned.
7446
7547
Example:
7648
.. code-block:: python
7749
78-
# Completely restrict chat
79-
app.restrict_chat(chat_id)
50+
from pyrogram import ChatPermissions
8051
81-
# All chat members can only send text messages
82-
app.restrict_chat(chat_id, can_send_messages=True)
52+
# Completely restrict chat
53+
app.set_chat_permissions(chat_id, ChatPermissions())
54+
55+
# Chat members can only send text messages, media, stickers and GIFs
56+
app.set_chat_permissions(
57+
chat_id,
58+
ChatPermissions(
59+
can_send_messages=True,
60+
can_send_media_messages=True,
61+
can_send_other_messages=True
62+
)
63+
)
8364
"""
8465
send_messages = True
8566
send_media = True
@@ -93,35 +74,35 @@ def restrict_chat(
9374
invite_users = True
9475
pin_messages = True
9576

96-
if can_send_messages:
77+
if permissions.can_send_messages:
9778
send_messages = None
9879

99-
if can_send_media_messages:
80+
if permissions.can_send_media_messages:
10081
send_messages = None
10182
send_media = None
10283

103-
if can_send_other_messages:
84+
if permissions.can_send_other_messages:
10485
send_messages = None
10586
send_stickers = None
10687
send_gifs = None
10788
send_games = None
10889
send_inline = None
10990

110-
if can_add_web_page_previews:
91+
if permissions.can_add_web_page_previews:
11192
send_messages = None
11293
embed_links = None
11394

114-
if can_send_polls:
95+
if permissions.can_send_polls:
11596
send_messages = None
11697
send_polls = None
11798

118-
if can_change_info:
99+
if permissions.can_change_info:
119100
change_info = None
120101

121-
if can_invite_users:
102+
if permissions.can_invite_users:
122103
invite_users = None
123104

124-
if can_pin_messages:
105+
if permissions.can_pin_messages:
125106
pin_messages = None
126107

127108
r = self.send(

pyrogram/client/types/user_and_chats/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Chat(Object):
9292
This field is available only in case *is_restricted* is True.
9393
9494
permissions (:obj:`ChatPermissions` *optional*):
95-
Information about the chat default permissions, for groups and supergroups.
95+
Default chat member permissions, for groups and supergroups.
9696
"""
9797

9898
def __init__(

0 commit comments

Comments
 (0)