From 631a0b1e13f9fd71941f402a7ba7bfdee6d66f96 Mon Sep 17 00:00:00 2001 From: Nadir Chowdhury Date: Mon, 19 Apr 2021 01:32:52 +0100 Subject: [PATCH] Add support for ApplicationFlags --- discord/client.py | 9 ++++++ discord/flags.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++ discord/state.py | 4 ++- docs/api.rst | 8 +++++ 4 files changed, 102 insertions(+), 1 deletion(-) diff --git a/discord/client.py b/discord/client.py index c3c15452b..4585e8a76 100644 --- a/discord/client.py +++ b/discord/client.py @@ -41,6 +41,7 @@ from .enums import ChannelType from .mentions import AllowedMentions from .errors import * from .enums import Status, VoiceRegion +from .flags import ApplicationFlags from .gateway import * from .activity import BaseActivity, create_activity from .voice_client import VoiceClient @@ -290,6 +291,14 @@ class Client: """ return self._connection.application_id + @property + def application_flags(self) -> ApplicationFlags: + """:class:`ApplicationFlags`: The client's application flags. + + .. versionadded: 2.0 + """ + return self._connection.application_flags # type: ignore + def is_ready(self): """:class:`bool`: Specifies if the client's internal cache is ready for use.""" return self._ready.is_set() diff --git a/discord/flags.py b/discord/flags.py index 5e182f8ff..bb3d5a3fc 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -34,11 +34,13 @@ __all__ = ( 'PublicUserFlags', 'Intents', 'MemberCacheFlags', + 'ApplicationFlags', ) FV = TypeVar('FV', bound='flag_value') BF = TypeVar('BF', bound='BaseFlags') + class flag_value(Generic[BF]): def __init__(self, func: Callable[[Any], int]): self.flag = func(None) @@ -63,16 +65,20 @@ class flag_value(Generic[BF]): def __repr__(self): return f'' + class alias_flag_value(flag_value): pass + def fill_with_flags(*, inverted: bool = False): def decorator(cls: Type[BF]): + # fmt: off cls.VALID_FLAGS = { name: value.flag for name, value in cls.__dict__.items() if isinstance(value, flag_value) } + # fmt: on if inverted: max_bits = max(cls.VALID_FLAGS.values()).bit_length() @@ -81,8 +87,10 @@ def fill_with_flags(*, inverted: bool = False): cls.DEFAULT_VALUE = 0 return cls + return decorator + # n.b. flags must inherit from this and use the decorator above class BaseFlags: VALID_FLAGS: ClassVar[Dict[str, int]] @@ -136,6 +144,7 @@ class BaseFlags: else: raise TypeError(f'Value to set for {self.__class__.__name__} must be a bool.') + @fill_with_flags(inverted=True) class SystemChannelFlags(BaseFlags): r"""Wraps up a Discord system channel flag value. @@ -270,6 +279,7 @@ class MessageFlags(BaseFlags): """ return 16 + @fill_with_flags() class PublicUserFlags(BaseFlags): r"""Wraps up the Discord User Public flags. @@ -808,6 +818,7 @@ class Intents(BaseFlags): """ return 1 << 14 + @fill_with_flags() class MemberCacheFlags(BaseFlags): """Controls the library's cache policy when it comes to members. @@ -936,3 +947,74 @@ class MemberCacheFlags(BaseFlags): @property def _voice_only(self): return self.value == 1 + + +@fill_with_flags() +class ApplicationFlags(BaseFlags): + r"""Wraps up the Discord Application flags. + + .. container:: operations + + .. describe:: x == y + + Checks if two ApplicationFlags are equal. + .. describe:: x != y + + Checks if two ApplicationFlags are not equal. + .. 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. + + .. versionadded:: 2.0 + + Attributes + ----------- + value: :class:`int` + The raw value. You should query flags via the properties + rather than using this raw value. + """ + + @flag_value + def gateway_presence(self): + """:class:`bool`: Returns ``True`` if the application is verified and is allowed to + receive presence information over the gateway. + """ + return 1 << 12 + + @flag_value + def gateway_presence_limited(self): + """:class:`bool`: Returns ``True`` if the application is allowed to receive limited + presence information over the gateway. + """ + return 1 << 13 + + @flag_value + def gateway_guild_members(self): + """:class:`bool`: Returns ``True`` if the application is verified and is allowed to + receive guild members information over the gateway. + """ + return 1 << 14 + + @flag_value + def gateway_guild_members_limited(self): + """:class:`bool`: Returns ``True`` if the application is allowed to receive limited + guild members information over the gateway. + """ + return 1 << 15 + + @flag_value + def verification_pending_guild_limit(self): + """:class:`bool`: Returns ``True`` if the application is currently pending verification + and has hit the guild limit. + """ + return 1 << 16 + + @flag_value + def embedded(self): + """:class:`bool`: Returns ``True`` if the application is embedded within the Discord client.""" + return 1 << 17 diff --git a/discord/state.py b/discord/state.py index 593c806b1..dd09634d3 100644 --- a/discord/state.py +++ b/discord/state.py @@ -48,7 +48,7 @@ from .member import Member from .role import Role from .enums import ChannelType, try_enum, Status from . import utils -from .flags import Intents, MemberCacheFlags +from .flags import ApplicationFlags, Intents, MemberCacheFlags from .object import Object from .invite import Invite from .interactions import Interaction @@ -452,6 +452,7 @@ class ConnectionState: pass else: self.application_id = utils._get_as_snowflake(application, 'id') + self.application_flags = ApplicationFlags._from_value(application['flags']) for guild_data in data['guilds']: self._add_guild_from_data(guild_data) @@ -1144,6 +1145,7 @@ class AutoShardedConnectionState(ConnectionState): pass else: self.application_id = utils._get_as_snowflake(application, 'id') + self.application_flags = ApplicationFlags._from_value(application['flags']) for guild_data in data['guilds']: self._add_guild_from_data(guild_data) diff --git a/docs/api.rst b/docs/api.rst index bf794285c..8dc0159d2 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3119,6 +3119,14 @@ MemberCacheFlags .. autoclass:: MemberCacheFlags :members: +ApplicationFlags +~~~~~~~~~~~~~~~~~ + +.. attributetable:: ApplicationFlags + +.. autoclass:: ApplicationFlags + :members: + File ~~~~~