From 6b641678623e1f26d8370852b72f4d6b404f3fd1 Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:44:03 +0200 Subject: [PATCH] Add support for role flags --- discord/flags.py | 64 +++++++++++++++++++++++++++++++++++++++++++ discord/role.py | 28 +++++++++++++------ discord/types/role.py | 1 + docs/api.rst | 8 ++++++ 4 files changed, 92 insertions(+), 9 deletions(-) diff --git a/discord/flags.py b/discord/flags.py index ab168e4d1..dc0508a0a 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -75,6 +75,7 @@ __all__ = ( 'ReadStateFlags', 'InviteFlags', 'AttachmentFlags', + 'RoleFlags', ) BF = TypeVar('BF', bound='BaseFlags') @@ -2726,3 +2727,66 @@ class AttachmentFlags(BaseFlags): def remix(self): """:class:`bool`: Returns ``True`` if the attachment has been edited using the remix feature.""" return 1 << 2 + + +@fill_with_flags() +class RoleFlags(BaseFlags): + r"""Wraps up the Discord Role flags + + .. versionadded:: 2.4 + + .. container:: operations + + .. describe:: x == y + + Checks if two RoleFlags are equal. + + .. describe:: x != y + + Checks if two RoleFlags are not equal. + + .. describe:: x | y, x |= y + + Returns a RoleFlags instance with all enabled flags from + both x and y. + + .. describe:: x & y, x &= y + + Returns a RoleFlags instance with only flags enabled on + both x and y. + + .. describe:: x ^ y, x ^= y + + Returns a RoleFlags instance with only flags enabled on + only one of x or y, not on both. + + .. describe:: ~x + + Returns a RoleFlags instance with all flags inverted from x. + + .. describe:: hash(x) + + Return the flag's hash. + + .. describe:: iter(x) + + Returns an iterator of ``(name, value)`` pairs. This allows it + to be, for example, constructed as a dict or a list of pairs. + Note that aliases are not shown. + + .. describe:: bool(b) + + Returns whether any flag is set to ``True``. + + + Attributes + ----------- + value: :class:`int` + The raw value. You should query flags via the properties + rather than using this raw value. + """ + + @flag_value + def in_prompt(self): + """:class:`bool`: Returns ``True`` if the role can be selected by members in an onboarding prompt.""" + return 1 << 0 diff --git a/discord/role.py b/discord/role.py index 79254d88d..3058c34f3 100644 --- a/discord/role.py +++ b/discord/role.py @@ -23,13 +23,15 @@ DEALINGS IN THE SOFTWARE. """ from __future__ import annotations -from typing import Any, Dict, List, Optional, Union, TYPE_CHECKING + +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union from .asset import Asset -from .permissions import Permissions from .colour import Colour +from .flags import RoleFlags from .mixins import Hashable -from .utils import snowflake_time, _get_as_snowflake, MISSING, _bytes_to_base64_data +from .permissions import Permissions +from .utils import MISSING, _bytes_to_base64_data, _get_as_snowflake, snowflake_time __all__ = ( 'RoleTags', @@ -38,15 +40,13 @@ __all__ = ( if TYPE_CHECKING: import datetime - from .types.role import ( - Role as RolePayload, - RoleTags as RoleTagPayload, - ) - from .types.guild import RolePositionUpdate + + from .abc import Snowflake from .guild import Guild from .member import Member from .state import ConnectionState - from .abc import Snowflake + from .types.guild import RolePositionUpdate + from .types.role import Role as RolePayload, RoleTags as RoleTagPayload class RoleTags: @@ -220,6 +220,7 @@ class Role(Hashable): 'hoist', 'guild', 'tags', + '_flags', '_state', ) @@ -282,6 +283,7 @@ class Role(Hashable): self.managed: bool = data.get('managed', False) self.mentionable: bool = data.get('mentionable', False) self.tags: Optional[RoleTags] + self._flags: int = data.get('flags', 0) try: self.tags = RoleTags(data['tags']) @@ -382,6 +384,14 @@ class Role(Hashable): role_id = self.id return [member for member in all_members if member._roles.has(role_id)] + @property + def flags(self) -> RoleFlags: + """:class:`RoleFlags`: Returns the role's flags. + + .. versionadded:: 2.4 + """ + return RoleFlags._from_value(self._flags) + async def _move(self, position: int, reason: Optional[str]) -> None: if position <= 0: raise ValueError("Cannot move role to position 0 or below") diff --git a/discord/types/role.py b/discord/types/role.py index 63e58fd0c..d32de8803 100644 --- a/discord/types/role.py +++ b/discord/types/role.py @@ -39,6 +39,7 @@ class Role(TypedDict): permissions: str managed: bool mentionable: bool + flags: int icon: NotRequired[Optional[str]] unicode_emoji: NotRequired[Optional[str]] tags: NotRequired[RoleTags] diff --git a/docs/api.rst b/docs/api.rst index 43e678d13..694c0a49a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -8058,6 +8058,14 @@ DirectoryEntry .. autoclass:: DirectoryEntry() :members: +RoleFlags +~~~~~~~~~~ + +.. attributetable:: RoleFlags + +.. autoclass:: RoleFlags + :members: + ForumTag ~~~~~~~~~