Browse Source

Add support for role flags

pull/10109/head
Soheab_ 2 years ago
committed by dolfies
parent
commit
6b64167862
  1. 64
      discord/flags.py
  2. 28
      discord/role.py
  3. 1
      discord/types/role.py
  4. 8
      docs/api.rst

64
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

28
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")

1
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]

8
docs/api.rst

@ -8058,6 +8058,14 @@ DirectoryEntry
.. autoclass:: DirectoryEntry()
:members:
RoleFlags
~~~~~~~~~~
.. attributetable:: RoleFlags
.. autoclass:: RoleFlags
:members:
ForumTag
~~~~~~~~~

Loading…
Cancel
Save