Add Convenience Shortcuts ChatPermissions.{all, no}_permissions (#2948)

This commit is contained in:
Poolitzer 2022-04-27 20:17:58 +02:00 committed by Hinrich Mahler
parent 3c8953cc5a
commit 97281da351
3 changed files with 49 additions and 2 deletions

View file

@ -4801,8 +4801,9 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
"""
Use this method to restrict a user in a supergroup. The bot must be an administrator in
the supergroup for this to work and must have the appropriate admin rights. Pass
:obj:`True` for all boolean parameters in :class:`telegram.ChatPermissions` to lift
restrictions from a user.
:obj:`True` for all boolean parameters to lift restrictions from a user.
.. seealso:: :meth:`telegram.ChatPermissions.all_permissions`
Args:
chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username

View file

@ -121,3 +121,25 @@ class ChatPermissions(TelegramObject):
self.can_invite_users,
self.can_pin_messages,
)
@classmethod
def all_permissions(cls) -> 'ChatPermissions':
"""
This method returns an :class:`ChatPermissions` instance with all attributes
set to :obj:`True`. This is e.g. useful when unrestricting a chat member with
:meth:`telegram.Bot.restrict_chat_member`.
.. versionadded:: 20.0
"""
return cls(True, True, True, True, True, True, True, True)
@classmethod
def no_permissions(cls) -> 'ChatPermissions':
"""
This method returns an :class:`ChatPermissions` instance
with all attributes set to :obj:`False`.
.. versionadded:: 20.0
"""
return cls(False, False, False, False, False, False, False, False)

View file

@ -124,3 +124,27 @@ class TestChatPermissions:
assert a != d
assert hash(a) != hash(d)
def test_all_permissions(self):
f = ChatPermissions()
t = ChatPermissions.all_permissions()
# if the dirs are the same, the attributes will all be there
assert dir(f) == dir(t)
# now we just need to check that all attributes are True. _id_attrs returns all values,
# if a new one is added without defaulting to True, this will fail
for key in t.__slots__:
assert t[key] is True
# and as a finisher, make sure the default is different.
assert f != t
def test_no_permissions(self):
f = ChatPermissions()
t = ChatPermissions.no_permissions()
# if the dirs are the same, the attributes will all be there
assert dir(f) == dir(t)
# now we just need to check that all attributes are True. _id_attrs returns all values,
# if a new one is added without defaulting to True, this will fail
for key in t.__slots__:
assert t[key] is False
# and as a finisher, make sure the default is different.
assert f != t