mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Added fields to ChatMember
This commit is contained in:
parent
de3c5c1a8d
commit
fe756faa2d
3 changed files with 64 additions and 0 deletions
|
|
@ -4,6 +4,7 @@ import datetime
|
|||
from typing import TYPE_CHECKING, Optional, Union
|
||||
|
||||
from .base import TelegramObject
|
||||
from ...utils import helper
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from .user import User
|
||||
|
|
@ -65,3 +66,31 @@ class ChatMember(TelegramObject):
|
|||
inline bots"""
|
||||
can_add_web_page_previews: Optional[bool] = None
|
||||
"""Restricted only. True, if the user is allowed to add web page previews to their messages"""
|
||||
|
||||
def is_chat_admin(self) -> bool:
|
||||
return ChatMemberStatus.is_chat_admin(self.status)
|
||||
|
||||
def is_chat_member(self) -> bool:
|
||||
return ChatMemberStatus.is_chat_member(self.status)
|
||||
|
||||
|
||||
class ChatMemberStatus(helper.Helper):
|
||||
"""
|
||||
Chat member status
|
||||
"""
|
||||
mode = helper.HelperMode.lowercase
|
||||
|
||||
CREATOR = helper.Item() # creator
|
||||
ADMINISTRATOR = helper.Item() # administrator
|
||||
MEMBER = helper.Item() # member
|
||||
RESTRICTED = helper.Item() # restricted
|
||||
LEFT = helper.Item() # left
|
||||
KICKED = helper.Item() # kicked
|
||||
|
||||
@classmethod
|
||||
def is_chat_admin(cls, role: str) -> bool:
|
||||
return role in [cls.ADMINISTRATOR, cls.CREATOR]
|
||||
|
||||
@classmethod
|
||||
def is_chat_member(cls, role: str) -> bool:
|
||||
return role in [cls.MEMBER, cls.ADMINISTRATOR, cls.CREATOR, cls.RESTRICTED]
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ This object contains information about one member of a chat.
|
|||
| `can_invite_users` | `#!python Optional[bool]` | Optional. Administrators and restricted only. True, if the user is allowed to invite new users to the chat |
|
||||
| `can_pin_messages` | `#!python Optional[bool]` | Optional. Administrators and restricted only. True, if the user is allowed to pin messages; groups and supergroups only |
|
||||
| `is_member` | `#!python Optional[bool]` | Optional. Restricted only. True, if the user is a member of the chat at the moment of the request |
|
||||
| `is_chat_admin` | `#!python bool` | True if the user is administrator or creator of the chat |
|
||||
| `can_send_messages` | `#!python Optional[bool]` | Optional. Restricted only. True, if the user is allowed to send text messages, contacts, locations and venues |
|
||||
| `can_send_media_messages` | `#!python Optional[bool]` | Optional. Restricted only. True, if the user is allowed to send audios, documents, photos, videos, video notes and voice notes |
|
||||
| `can_send_polls` | `#!python Optional[bool]` | Optional. Restricted only. True, if the user is allowed to send polls |
|
||||
|
|
|
|||
34
tests/test_api/test_types/test_chat_member.py
Normal file
34
tests/test_api/test_types/test_chat_member.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import pytest
|
||||
|
||||
from aiogram.api.types import ChatMember, User
|
||||
|
||||
user = User(id=42, is_bot=False, first_name="User", last_name=None)
|
||||
|
||||
|
||||
class TestChatMember:
|
||||
@pytest.mark.parametrize(
|
||||
"status,result",
|
||||
[
|
||||
["administrator", True],
|
||||
["creator", True],
|
||||
["member", False]
|
||||
]
|
||||
)
|
||||
def test_is_chat_admin(self, status: str, result: bool):
|
||||
chat_member = ChatMember(user=user, status=status)
|
||||
assert chat_member.is_chat_admin() == result
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"status,result",
|
||||
[
|
||||
["administrator", True],
|
||||
["creator", True],
|
||||
["member", True],
|
||||
["restricted", True],
|
||||
["kicked", False],
|
||||
["left", False]
|
||||
]
|
||||
)
|
||||
def test_is_chat_member(self, status: str, result: bool):
|
||||
chat_member = ChatMember(user=user, status=status)
|
||||
assert chat_member.is_chat_member() == result
|
||||
Loading…
Add table
Add a link
Reference in a new issue