Source code for discord.flags

"""
The MIT License (MIT)

Copyright (c) 2015-2021 Rapptz
Copyright (c) 2021-present Pycord Development

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""

from __future__ import annotations

from typing import Any, Callable, ClassVar, Iterator, TypeVar, overload

from .enums import UserFlags

__all__ = (
    "SystemChannelFlags",
    "MessageFlags",
    "AttachmentFlags",
    "PublicUserFlags",
    "Intents",
    "MemberCacheFlags",
    "ApplicationFlags",
    "ChannelFlags",
    "SKUFlags",
    "RoleFlags",
    "MemberFlags",
)

FV = TypeVar("FV", bound="flag_value")
BF = TypeVar("BF", bound="BaseFlags")


class flag_value:
    def __init__(self, func: Callable[[Any], int]):
        self.flag = func(None)
        self.__doc__ = func.__doc__

    @overload
    def __get__(self: FV, instance: None, owner: type[BF]) -> FV: ...

    @overload
    def __get__(self, instance: BF, owner: type[BF]) -> bool: ...

    def __get__(self, instance: BF | None, owner: type[BF]) -> Any:
        if instance is None:
            return self
        return instance._has_flag(self.flag)

    def __set__(self, instance: BF, value: bool) -> None:
        instance._set_flag(self.flag, value)

    def __repr__(self):
        return f"<flag_value flag={self.flag!r}>"


class alias_flag_value(flag_value):
    pass


def fill_with_flags(*, inverted: bool = False):
    def decorator(cls: type[BF]):
        cls.VALID_FLAGS = {
            name: value.flag
            for name, value in cls.__dict__.items()
            if isinstance(value, flag_value)
        }

        if inverted:
            max_bits = max(cls.VALID_FLAGS.values()).bit_length()
            cls.DEFAULT_VALUE = -1 + (2**max_bits)
        else:
            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]]
    DEFAULT_VALUE: ClassVar[int]

    value: int

    __slots__ = ("value",)

    def __init__(self, **kwargs: bool):
        self.value = self.DEFAULT_VALUE
        for key, value in kwargs.items():
            if key not in self.VALID_FLAGS:
                raise TypeError(f"{key!r} is not a valid flag name.")
            setattr(self, key, value)

    @classmethod
    def _from_value(cls, value):
        self = cls.__new__(cls)
        self.value = value
        return self

    def __eq__(self, other: Any) -> bool:
        return isinstance(other, self.__class__) and self.value == other.value

    def __hash__(self) -> int:
        return hash(self.value)

    def __repr__(self) -> str:
        return f"<{self.__class__.__name__} value={self.value}>"

    def __iter__(self) -> Iterator[tuple[str, bool]]:
        for name, value in self.__class__.__dict__.items():
            if isinstance(value, alias_flag_value):
                continue

            if isinstance(value, flag_value):
                yield name, self._has_flag(value.flag)

    def __and__(self, other):
        if isinstance(other, self.__class__):
            return self.__class__._from_value(self.value & other.value)
        elif isinstance(other, flag_value):
            return self.__class__._from_value(self.value & other.flag)
        else:
            raise TypeError(
                f"'&' not supported between instances of {type(self)} and {type(other)}"
            )

    def __or__(self, other):
        if isinstance(other, self.__class__):
            return self.__class__._from_value(self.value | other.value)
        elif isinstance(other, flag_value):
            return self.__class__._from_value(self.value | other.flag)
        else:
            raise TypeError(
                f"'|' not supported between instances of {type(self)} and {type(other)}"
            )

    def __add__(self, other):
        try:
            return self | other
        except TypeError:
            raise TypeError(
                f"'+' not supported between instances of {type(self)} and {type(other)}"
            )

    def __sub__(self, other):
        if isinstance(other, self.__class__):
            return self.__class__._from_value(self.value & ~other.value)
        elif isinstance(other, flag_value):
            return self.__class__._from_value(self.value & ~other.flag)
        else:
            raise TypeError(
                f"'-' not supported between instances of {type(self)} and {type(other)}"
            )

    def __invert__(self):
        return self.__class__._from_value(~self.value)

    __rand__: Callable[[BaseFlags | flag_value], bool] = __and__
    __ror__: Callable[[BaseFlags | flag_value], bool] = __or__
    __radd__: Callable[[BaseFlags | flag_value], bool] = __add__
    __rsub__: Callable[[BaseFlags | flag_value], bool] = __sub__

    def _has_flag(self, o: int) -> bool:
        return (self.value & o) == o

    def _set_flag(self, o: int, toggle: bool) -> None:
        if toggle:
            self.value |= o
        else:
            self.value &= ~o


[docs]@fill_with_flags(inverted=True) class SystemChannelFlags(BaseFlags): r"""Wraps up a Discord system channel flag value. Similar to :class:`Permissions`\, the properties provided are two way. You can set and retrieve individual bits using the properties as if they were regular bools. This allows you to edit the system flags easily. To construct an object you can pass keyword arguments denoting the flags to enable or disable. .. container:: operations .. describe:: x == y Checks if two flags are equal. .. describe:: x != y Checks if two flags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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. Attributes ----------- value: :class:`int` The raw value. This value is a bit array field of a 53-bit integer representing the currently available flags. You should query flags via the properties rather than using this raw value. """ __slots__ = () # For some reason the flags for system channels are "inverted" # ergo, if they're set then it means "suppress" (off in the GUI toggle) # Since this is counter-intuitive from an API perspective and annoying # these will be inverted automatically def _has_flag(self, o: int) -> bool: return (self.value & o) != o def _set_flag(self, o: int, toggle: bool) -> None: if toggle: self.value &= ~o else: self.value |= o
[docs] @flag_value def join_notifications(self): """:class:`bool`: Returns ``True`` if the system channel is used for member join notifications.""" return 1
[docs] @flag_value def premium_subscriptions(self): """:class:`bool`: Returns ``True`` if the system channel is used for "Nitro boosting" notifications.""" return 2
[docs] @flag_value def guild_reminder_notifications(self): """:class:`bool`: Returns ``True`` if the system channel is used for server setup helpful tips notifications. .. versionadded:: 2.0 """ return 4
[docs] @flag_value def join_notification_replies(self): """:class:`bool`: Returns ``True`` if the system channel is allowing member join sticker replies. .. versionadded:: 2.0 """ return 8
[docs]@fill_with_flags() class MessageFlags(BaseFlags): r"""Wraps up a Discord Message flag value. See :class:`SystemChannelFlags`. .. container:: operations .. describe:: x == y Checks if two flags are equal. .. describe:: x != y Checks if two flags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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. .. versionadded:: 1.3 Attributes ----------- value: :class:`int` The raw value. This value is a bit array field of a 53-bit integer representing the currently available flags. You should query flags via the properties rather than using this raw value. """ __slots__ = ()
[docs] @flag_value def crossposted(self): """:class:`bool`: Returns ``True`` if the message is the original crossposted message.""" return 1
[docs] @flag_value def is_crossposted(self): """:class:`bool`: Returns ``True`` if the message was crossposted from another channel.""" return 2
[docs] @flag_value def suppress_embeds(self): """:class:`bool`: Returns ``True`` if the message's embeds have been suppressed.""" return 4
[docs] @flag_value def source_message_deleted(self): """:class:`bool`: Returns ``True`` if the source message for this crosspost has been deleted.""" return 8
[docs] @flag_value def urgent(self): """:class:`bool`: Returns ``True`` if the source message is an urgent message. An urgent message is one sent by Discord Trust and Safety. """ return 16
[docs] @flag_value def has_thread(self): """:class:`bool`: Returns ``True`` if the source message is associated with a thread. .. versionadded:: 2.0 """ return 32
[docs] @flag_value def ephemeral(self): """:class:`bool`: Returns ``True`` if the source message is ephemeral. .. versionadded:: 2.0 """ return 64
[docs] @flag_value def loading(self): """:class:`bool`: Returns ``True`` if the source message is deferred. The user sees a 'thinking' state. .. versionadded:: 2.0 """ return 128
[docs] @flag_value def failed_to_mention_some_roles_in_thread(self): """:class:`bool`: Returns ``True`` if some roles are failed to mention in a thread. .. versionadded:: 2.0 """ return 256
[docs] @flag_value def suppress_notifications(self): """:class:`bool`: Returns ``True`` if the source message does not trigger push and desktop notifications. Users will still receive mentions. .. versionadded:: 2.4 """ return 4096
[docs] @flag_value def is_voice_message(self): """:class:`bool`: Returns ``True`` if this message is a voice message. .. versionadded:: 2.5 """ return 8192
[docs]@fill_with_flags() class PublicUserFlags(BaseFlags): r"""Wraps up the Discord User Public flags. .. container:: operations .. describe:: x == y Checks if two PublicUserFlags are equal. .. describe:: x != y Checks if two PublicUserFlags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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:: 1.4 Attributes ----------- value: :class:`int` The raw value. This value is a bit array field of a 53-bit integer representing the currently available flags. You should query flags via the properties rather than using this raw value. """ __slots__ = ()
[docs] @flag_value def staff(self): """:class:`bool`: Returns ``True`` if the user is a Discord Employee.""" return UserFlags.staff.value
[docs] @flag_value def partner(self): """:class:`bool`: Returns ``True`` if the user is a Discord Partner.""" return UserFlags.partner.value
[docs] @flag_value def hypesquad(self): """:class:`bool`: Returns ``True`` if the user is a HypeSquad Events member.""" return UserFlags.hypesquad.value
[docs] @flag_value def bug_hunter(self): """:class:`bool`: Returns ``True`` if the user is a Bug Hunter""" return UserFlags.bug_hunter.value
[docs] @flag_value def premium_promo_dismissed(self): """:class:`bool`: Returns ``True`` if the user is marked as dismissed Nitro promotion""" return UserFlags.premium_promo_dismissed.value
[docs] @flag_value def hypesquad_bravery(self): """:class:`bool`: Returns ``True`` if the user is a HypeSquad Bravery member.""" return UserFlags.hypesquad_bravery.value
[docs] @flag_value def hypesquad_brilliance(self): """:class:`bool`: Returns ``True`` if the user is a HypeSquad Brilliance member.""" return UserFlags.hypesquad_brilliance.value
[docs] @flag_value def hypesquad_balance(self): """:class:`bool`: Returns ``True`` if the user is a HypeSquad Balance member.""" return UserFlags.hypesquad_balance.value
[docs] @flag_value def early_supporter(self): """:class:`bool`: Returns ``True`` if the user is an Early Supporter.""" return UserFlags.early_supporter.value
[docs] @flag_value def team_user(self): """:class:`bool`: Returns ``True`` if the user is a Team User.""" return UserFlags.team_user.value
[docs] @flag_value def system(self): """:class:`bool`: Returns ``True`` if the user is a system user (i.e. represents Discord officially).""" return UserFlags.system.value
[docs] @flag_value def bug_hunter_level_2(self): """:class:`bool`: Returns ``True`` if the user is a Bug Hunter Level 2""" return UserFlags.bug_hunter_level_2.value
[docs] @flag_value def verified_bot(self): """:class:`bool`: Returns ``True`` if the user is a Verified Bot.""" return UserFlags.verified_bot.value
[docs] @flag_value def verified_bot_developer(self): """:class:`bool`: Returns ``True`` if the user is an Early Verified Bot Developer.""" return UserFlags.verified_bot_developer.value
[docs] @alias_flag_value def early_verified_bot_developer(self): """:class:`bool`: An alias for :attr:`verified_bot_developer`. .. versionadded:: 1.5 """ return UserFlags.verified_bot_developer.value
[docs] @flag_value def discord_certified_moderator(self): """:class:`bool`: Returns ``True`` if the user is a Discord Certified Moderator. .. versionadded:: 2.0 """ return UserFlags.discord_certified_moderator.value
[docs] @flag_value def bot_http_interactions(self): """:class:`bool`: Returns ``True`` if the bot has set an interactions endpoint url. .. versionadded:: 2.0 """ return UserFlags.bot_http_interactions.value
[docs] @flag_value def active_developer(self): """:class:`bool`: Returns ``True`` if the user is an Active Developer. .. versionadded:: 2.3 """ return UserFlags.active_developer.value
[docs] def all(self) -> list[UserFlags]: """List[:class:`UserFlags`]: Returns all public flags the user has.""" return [ public_flag for public_flag in UserFlags if self._has_flag(public_flag.value) ]
[docs]@fill_with_flags() class Intents(BaseFlags): r"""Wraps up a Discord gateway intent flag. Similar to :class:`Permissions`\, the properties provided are two way. You can set and retrieve individual bits using the properties as if they were regular bools. To construct an object you can pass keyword arguments denoting the flags to enable or disable. This is used to disable certain gateway features that are unnecessary to run your bot. To make use of this, it is passed to the ``intents`` keyword argument of :class:`Client`. .. versionadded:: 1.5 .. container:: operations .. describe:: x == y Checks if two flags are equal. .. describe:: x != y Checks if two flags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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. Attributes ----------- value: :class:`int` The raw value. You should query flags via the properties rather than using this raw value. """ __slots__ = () def __init__(self, **kwargs: bool): self.value = self.DEFAULT_VALUE for key, value in kwargs.items(): if key not in self.VALID_FLAGS: raise TypeError(f"{key!r} is not a valid flag name.") setattr(self, key, value)
[docs] @classmethod def all(cls: type[Intents]) -> Intents: """A factory method that creates a :class:`Intents` with everything enabled.""" value = sum({1 << (flag.bit_length() - 1) for flag in cls.VALID_FLAGS.values()}) self = cls.__new__(cls) self.value = value return self
[docs] @classmethod def none(cls: type[Intents]) -> Intents: """A factory method that creates a :class:`Intents` with everything disabled.""" self = cls.__new__(cls) self.value = self.DEFAULT_VALUE return self
[docs] @classmethod def default(cls: type[Intents]) -> Intents: """A factory method that creates a :class:`Intents` with everything enabled except :attr:`presences`, :attr:`members`, and :attr:`message_content`. """ self = cls.all() self.presences = False self.members = False self.message_content = False return self
[docs] @flag_value def guilds(self): """:class:`bool`: Whether guild related events are enabled. This corresponds to the following events: - :func:`on_guild_join` - :func:`on_guild_remove` - :func:`on_guild_available` - :func:`on_guild_unavailable` - :func:`on_guild_channel_update` - :func:`on_guild_channel_create` - :func:`on_guild_channel_delete` - :func:`on_guild_channel_pins_update` This also corresponds to the following attributes and classes in terms of cache: - :attr:`Client.guilds` - :class:`Guild` and all its attributes. - :meth:`Client.get_channel` - :meth:`Client.get_all_channels` It is highly advisable to leave this intent enabled for your bot to function. """ return 1 << 0
[docs] @flag_value def members(self): """:class:`bool`: Whether guild member related events are enabled. This corresponds to the following events: - :func:`on_member_join` - :func:`on_member_remove` - :func:`on_raw_member_remove` - :func:`on_member_update` - :func:`on_user_update` This also corresponds to the following attributes and classes in terms of cache: - :meth:`Client.get_all_members` - :meth:`Client.get_user` - :meth:`Guild.chunk` - :meth:`Guild.fetch_members` - :meth:`Guild.get_member` - :attr:`Guild.members` - :attr:`Member.roles` - :attr:`Member.nick` - :attr:`Member.premium_since` - :attr:`User.name` - :attr:`User.avatar` - :attr:`User.discriminator` For more information go to the :ref:`member intent documentation <need_members_intent>`. .. note:: This intent is privileged, meaning that bots in over 100 guilds that require this intent would need to request this intent on the Developer Portal. """ return 1 << 1
[docs] @alias_flag_value def bans(self): """:class:`bool`: Alias of :attr:`.moderation`. .. versionchanged:: 2.5 Changed to an alias. """ return 1 << 2
[docs] @flag_value def moderation(self): """:class:`bool`: Whether guild moderation related events are enabled. This corresponds to the following events: - :func:`on_audit_log_entry` - :func:`on_member_ban` - :func:`on_member_unban` This does not correspond to any attributes or classes in the library in terms of cache. """ return 1 << 2
[docs] @flag_value def emojis(self): """:class:`bool`: Alias of :attr:`.emojis_and_stickers`. .. versionchanged:: 2.0 Changed to an alias. """ return 1 << 3
[docs] @alias_flag_value def emojis_and_stickers(self): """:class:`bool`: Whether guild emoji and sticker related events are enabled. .. versionadded:: 2.0 This corresponds to the following events: - :func:`on_guild_emojis_update` - :func:`on_guild_stickers_update` This also corresponds to the following attributes and classes in terms of cache: - :class:`Emoji` - :class:`GuildSticker` - :meth:`Client.get_emoji` - :meth:`Client.get_sticker` - :meth:`Client.emojis` - :meth:`Client.stickers` - :attr:`Guild.emojis` - :attr:`Guild.stickers` """ return 1 << 3
[docs] @flag_value def integrations(self): """:class:`bool`: Whether guild integration related events are enabled. This corresponds to the following events: - :func:`on_guild_integrations_update` - :func:`on_integration_create` - :func:`on_integration_update` - :func:`on_raw_integration_delete` This does not correspond to any attributes or classes in the library in terms of cache. """ return 1 << 4
[docs] @flag_value def webhooks(self): """:class:`bool`: Whether guild webhook related events are enabled. This corresponds to the following events: - :func:`on_webhooks_update` This does not correspond to any attributes or classes in the library in terms of cache. """ return 1 << 5
[docs] @flag_value def invites(self): """:class:`bool`: Whether guild invite related events are enabled. This corresponds to the following events: - :func:`on_invite_create` - :func:`on_invite_delete` This does not correspond to any attributes or classes in the library in terms of cache. """ return 1 << 6
[docs] @flag_value def voice_states(self): """:class:`bool`: Whether guild voice state related events are enabled. This corresponds to the following events: - :func:`on_voice_state_update` This also corresponds to the following attributes and classes in terms of cache: - :attr:`VoiceChannel.members` - :attr:`VoiceChannel.voice_states` - :attr:`StageChannel.members` - :attr:`StageChannel.speakers` - :attr:`StageChannel.listeners` - :attr:`StageChannel.moderators` - :attr:`StageChannel.voice_states` - :attr:`Member.voice` .. note:: This intent is required to connect to voice. """ return 1 << 7
[docs] @flag_value def presences(self): """:class:`bool`: Whether guild presence related events are enabled. This corresponds to the following events: - :func:`on_presence_update` This also corresponds to the following attributes and classes in terms of cache: - :attr:`Member.activities` - :attr:`Member.status` - :attr:`Member.raw_status` For more information go to the :ref:`presence intent documentation <need_presence_intent>`. .. note:: This intent is privileged, meaning that bots in over 100 guilds that require this intent would need to request this intent on the Developer Portal. """ return 1 << 8
[docs] @alias_flag_value def messages(self): """:class:`bool`: Whether guild and direct message related events are enabled. This is a shortcut to set or get both :attr:`guild_messages` and :attr:`dm_messages`. This corresponds to the following events: - :func:`on_message` (both guilds and DMs) - :func:`on_message_edit` (both guilds and DMs) - :func:`on_message_delete` (both guilds and DMs) - :func:`on_raw_message_delete` (both guilds and DMs) - :func:`on_raw_message_edit` (both guilds and DMs) This also corresponds to the following attributes and classes in terms of cache: - :class:`Message` - :attr:`Client.cached_messages` - :meth:`Client.get_message` - :attr:`Client.polls` - :meth:`Client.get_poll` Note that due to an implicit relationship this also corresponds to the following events: - :func:`on_reaction_add` (both guilds and DMs) - :func:`on_reaction_remove` (both guilds and DMs) - :func:`on_reaction_clear` (both guilds and DMs) .. note:: :attr:`message_content` is required to receive the actual content of guild messages. """ return (1 << 9) | (1 << 12)
[docs] @flag_value def guild_messages(self): """:class:`bool`: Whether guild message related events are enabled. See also :attr:`dm_messages` for DMs or :attr:`messages` for both. This corresponds to the following events: - :func:`on_message` (only for guilds) - :func:`on_message_edit` (only for guilds) - :func:`on_message_delete` (only for guilds) - :func:`on_raw_message_delete` (only for guilds) - :func:`on_raw_message_edit` (only for guilds) This also corresponds to the following attributes and classes in terms of cache: - :class:`Message` - :attr:`Client.cached_messages` (only for guilds) - :meth:`Client.get_message` (only for guilds) - :attr:`Client.polls` (only for guilds) - :meth:`Client.get_poll` (only for guilds) Note that due to an implicit relationship this also corresponds to the following events: - :func:`on_reaction_add` (only for guilds) - :func:`on_reaction_remove` (only for guilds) - :func:`on_reaction_clear` (only for guilds) Without the :attr:`message_content` intent enabled, the following fields are either an empty string or empty array: - :attr:`Message.content` - :attr:`Message.embeds` - :attr:`Message.attachments` - :attr:`Message.components` - :attr:`Message.poll` For more information go to the :ref:`message content intent documentation <need_message_content_intent>`. """ return 1 << 9
[docs] @flag_value def dm_messages(self): """:class:`bool`: Whether direct message related events are enabled. See also :attr:`guild_messages` for guilds or :attr:`messages` for both. This corresponds to the following events: - :func:`on_message` (only for DMs) - :func:`on_message_edit` (only for DMs) - :func:`on_message_delete` (only for DMs) - :func:`on_raw_message_delete` (only for DMs) - :func:`on_raw_message_edit` (only for DMs) This also corresponds to the following attributes and classes in terms of cache: - :class:`Message` - :attr:`Client.cached_messages` (only for DMs) - :meth:`Client.get_message` (only for DMs) - :attr:`Client.polls` (only for DMs) - :meth:`Client.get_poll` (only for DMs) Note that due to an implicit relationship this also corresponds to the following events: - :func:`on_reaction_add` (only for DMs) - :func:`on_reaction_remove` (only for DMs) - :func:`on_reaction_clear` (only for DMs) """ return 1 << 12
[docs] @alias_flag_value def reactions(self): """:class:`bool`: Whether guild and direct message reaction related events are enabled. This is a shortcut to set or get both :attr:`guild_reactions` and :attr:`dm_reactions`. This corresponds to the following events: - :func:`on_reaction_add` (both guilds and DMs) - :func:`on_reaction_remove` (both guilds and DMs) - :func:`on_reaction_clear` (both guilds and DMs) - :func:`on_raw_reaction_add` (both guilds and DMs) - :func:`on_raw_reaction_remove` (both guilds and DMs) - :func:`on_raw_reaction_clear` (both guilds and DMs) This also corresponds to the following attributes and classes in terms of cache: - :attr:`Message.reactions` (both guild and DM messages) """ return (1 << 10) | (1 << 13)
[docs] @flag_value def guild_reactions(self): """:class:`bool`: Whether guild message reaction related events are enabled. See also :attr:`dm_reactions` for DMs or :attr:`reactions` for both. This corresponds to the following events: - :func:`on_reaction_add` (only for guilds) - :func:`on_reaction_remove` (only for guilds) - :func:`on_reaction_clear` (only for guilds) - :func:`on_raw_reaction_add` (only for guilds) - :func:`on_raw_reaction_remove` (only for guilds) - :func:`on_raw_reaction_clear` (only for guilds) This also corresponds to the following attributes and classes in terms of cache: - :attr:`Message.reactions` (only for guild messages) """ return 1 << 10
[docs] @flag_value def dm_reactions(self): """:class:`bool`: Whether direct message reaction related events are enabled. See also :attr:`guild_reactions` for guilds or :attr:`reactions` for both. This corresponds to the following events: - :func:`on_reaction_add` (only for DMs) - :func:`on_reaction_remove` (only for DMs) - :func:`on_reaction_clear` (only for DMs) - :func:`on_raw_reaction_add` (only for DMs) - :func:`on_raw_reaction_remove` (only for DMs) - :func:`on_raw_reaction_clear` (only for DMs) This also corresponds to the following attributes and classes in terms of cache: - :attr:`Message.reactions` (only for DM messages) """ return 1 << 13
[docs] @alias_flag_value def typing(self): """:class:`bool`: Whether guild and direct message typing related events are enabled. This is a shortcut to set or get both :attr:`guild_typing` and :attr:`dm_typing`. This corresponds to the following events: - :func:`on_typing` (both guilds and DMs) This does not correspond to any attributes or classes in the library in terms of cache. """ return (1 << 11) | (1 << 14)
[docs] @flag_value def guild_typing(self): """:class:`bool`: Whether guild and direct message typing related events are enabled. See also :attr:`dm_typing` for DMs or :attr:`typing` for both. This corresponds to the following events: - :func:`on_typing` (only for guilds) This does not correspond to any attributes or classes in the library in terms of cache. """ return 1 << 11
[docs] @flag_value def dm_typing(self): """:class:`bool`: Whether guild and direct message typing related events are enabled. See also :attr:`guild_typing` for guilds or :attr:`typing` for both. This corresponds to the following events: - :func:`on_typing` (only for DMs) This does not correspond to any attributes or classes in the library in terms of cache. """ return 1 << 14
[docs] @flag_value def message_content(self): """:class:`bool`: Whether the bot will receive message content in guild messages. This corresponds to the following attributes: - :attr:`Message.content` - :attr:`Message.embeds` - :attr:`Message.attachments` - :attr:`Message.components` - :attr:`Message.poll` These attributes will still be available for messages received from interactions, the bot's own messages, messages the bot was mentioned in, and DMs. .. versionadded:: 2.0 .. note:: As of September 2022 using this intent requires opting in explicitly via the Developer Portal to receive the actual content of the guild messages. This intent is privileged, meaning that bots in over 100 guilds that require this intent would need to request this intent on the Developer Portal. See https://support-dev.discord.com/hc/en-us/articles/4404772028055 for more information. """ return 1 << 15
[docs] @flag_value def scheduled_events(self): """:class:`bool`: Whether "scheduled event" related events are enabled. This corresponds to the following events: - :func:`on_scheduled_event_create` - :func:`on_scheduled_event_update` - :func:`on_scheduled_event_delete` - :func:`on_scheduled_event_user_add` - :func:`on_raw_scheduled_event_user_add` - :func:`on_scheduled_event_user_remove` - :func:`on_raw_scheduled_event_user_remove` This also corresponds to the following attributes and classes in terms of cache: - :class:`ScheduledEvent` - :meth:`Guild.get_scheduled_event` """ return 1 << 16
[docs] @flag_value def auto_moderation_configuration(self): """:class:`bool`: Whether guild auto moderation configuration events are enabled. This corresponds to the following events: - :func:`on_auto_moderation_rule_create` - :func:`on_auto_moderation_rule_update` - :func:`on_auto_moderation_rule_delete` """ return 1 << 20
[docs] @flag_value def auto_moderation_execution(self): """:class:`bool`: Whether guild auto moderation execution events are enabled. This corresponds to the following events: - :func:`on_auto_moderation_action_execution` """ return 1 << 21
[docs] @flag_value def guild_polls(self): """:class:`bool`: Whether poll-related events in guilds are enabled. See also :attr:`dm_polls` for DMs or :attr:`polls` for both. This corresponds to the following events: - :func:`on_poll_vote_add` (only for guilds) - :func:`on_poll_vote_remove` (only for guilds) - :func:`on_raw_poll_vote_add` (only for guilds) - :func:`on_raw_poll_vote_remove` (only for guilds) This also corresponds to the following attributes and classes in terms of cache: - :attr:`PollAnswer.count` (only for guild polls) - :attr:`PollResults.answer_counts` (only for guild polls) """ return 1 << 24
[docs] @flag_value def dm_polls(self): """:class:`bool`: Whether poll-related events in direct messages are enabled. See also :attr:`guild_polls` for guilds or :attr:`polls` for both. This corresponds to the following events: - :func:`on_poll_vote_add` (only for DMs) - :func:`on_poll_vote_remove` (only for DMs) - :func:`on_raw_poll_vote_add` (only for DMs) - :func:`on_raw_poll_vote_remove` (only for DMs) This also corresponds to the following attributes and classes in terms of cache: - :attr:`PollAnswer.count` (only for DM polls) - :attr:`PollResults.answer_counts` (only for DM polls) """ return 1 << 25
[docs] @alias_flag_value def polls(self): """:class:`bool`: Whether poll-related events in guilds and direct messages are enabled. This is a shortcut to set or get both :attr:`guild_polls` and :attr:`dm_polls`. This corresponds to the following events: - :func:`on_poll_vote_add` (both guilds and DMs) - :func:`on_poll_vote_remove` (both guilds and DMs) - :func:`on_raw_poll_vote_add` (both guilds and DMs) - :func:`on_raw_poll_vote_remove` (both guilds and DMs) This also corresponds to the following attributes and classes in terms of cache: - :attr:`PollAnswer.count` (both guild and DM polls) - :attr:`PollResults.answer_counts` (both guild and DM polls) """ return (1 << 24) | (1 << 25)
[docs]@fill_with_flags() class MemberCacheFlags(BaseFlags): """Controls the library's cache policy when it comes to members. This allows for finer grained control over what members are cached. Note that the bot's own member is always cached. This class is passed to the ``member_cache_flags`` parameter in :class:`Client`. Due to a quirk in how Discord works, in order to ensure proper cleanup of cache resources it is recommended to have :attr:`Intents.members` enabled. Otherwise, the library cannot know when a member leaves a guild and is thus unable to clean up after itself. To construct an object you can pass keyword arguments denoting the flags to enable or disable. The default value is all flags enabled. .. versionadded:: 1.5 .. container:: operations .. describe:: x == y Checks if two flags are equal. .. describe:: x != y Checks if two flags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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. Attributes ---------- value: :class:`int` The raw value. You should query flags via the properties rather than using this raw value. """ __slots__ = () def __init__(self, **kwargs: bool): bits = max(self.VALID_FLAGS.values()).bit_length() self.value = (1 << bits) - 1 for key, value in kwargs.items(): if key not in self.VALID_FLAGS: raise TypeError(f"{key!r} is not a valid flag name.") setattr(self, key, value)
[docs] @classmethod def all(cls: type[MemberCacheFlags]) -> MemberCacheFlags: """A factory method that creates a :class:`MemberCacheFlags` with everything enabled.""" bits = max(cls.VALID_FLAGS.values()).bit_length() value = (1 << bits) - 1 self = cls.__new__(cls) self.value = value return self
[docs] @classmethod def none(cls: type[MemberCacheFlags]) -> MemberCacheFlags: """A factory method that creates a :class:`MemberCacheFlags` with everything disabled.""" self = cls.__new__(cls) self.value = self.DEFAULT_VALUE return self
@property def _empty(self): return self.value == self.DEFAULT_VALUE
[docs] @flag_value def voice(self): """:class:`bool`: Whether to cache members that are in voice. This requires :attr:`Intents.voice_states`. Members that leave voice are no longer cached. """ return 1
[docs] @flag_value def joined(self): """:class:`bool`: Whether to cache members that joined the guild or are chunked as part of the initial log in flow. This requires :attr:`Intents.members`. Members that leave the guild are no longer cached. """ return 2
[docs] @flag_value def interaction(self): """:class:`bool`: Whether to cache members obtained through interactions. This includes members received through :class:`discord.Interaction` and :class:`discord.Option`. """ return 4
[docs] @classmethod def from_intents(cls: type[MemberCacheFlags], intents: Intents) -> MemberCacheFlags: """A factory method that creates a :class:`MemberCacheFlags` based on the currently selected :class:`Intents`. Parameters ---------- intents: :class:`Intents` The intents to select from. Returns ------- :class:`MemberCacheFlags` The resulting member cache flags. """ self = cls.none() self.interaction = True if intents.members: self.joined = True if intents.voice_states: self.voice = True return self
def _verify_intents(self, intents: Intents): if self.voice and not intents.voice_states: raise ValueError("MemberCacheFlags.voice requires Intents.voice_states") if self.joined and not intents.members: raise ValueError("MemberCacheFlags.joined requires Intents.members") @property def _voice_only(self): return self.value == 1
[docs]@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:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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. """
[docs] @flag_value def managed_emoji(self): """:class:`bool`: Returns ``True`` if the application is a managed emoji.""" return 1 << 2
[docs] @flag_value def group_dm_create(self): """:class:`bool`: Returns ``True`` if the application can create group DMs.""" return 1 << 5
[docs] @flag_value def application_auto_moderation_rule_create_badge(self): """:class:`bool`: Returns ``True`` if the application uses the Auto Moderation API. .. versionadded:: 2.5 """ return 1 << 6
[docs] @flag_value def rpc_has_connected(self): """:class:`bool`: Returns ``True`` if the application has connected to RPC.""" return 1 << 11
[docs] @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
[docs] @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
[docs] @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
[docs] @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
[docs] @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
[docs] @flag_value def embedded(self): """:class:`bool`: Returns ``True`` if the application is embedded within the Discord client.""" return 1 << 17
[docs] @flag_value def gateway_message_content(self): """:class:`bool`: Returns ``True`` if the application is allowed to read message contents in guilds.""" return 1 << 18
[docs] @flag_value def gateway_message_content_limited(self): """:class:`bool`: Returns ``True`` if the application is currently pending verification and has hit the guild limit. """ return 1 << 19
[docs] @flag_value def app_commands_badge(self): """:class:`bool`: Returns ``True`` if the application has registered at least one global application command, and by extension has the badge. .. versionadded:: 2.1 """ return 1 << 23
[docs] @flag_value def active(self): """:class:`bool`: Returns ``True`` if the app is considered active. Applications are considered active if they have had any command executions in the past 30 days. .. versionadded:: 2.3 """ return 1 << 24
[docs]@fill_with_flags() class ChannelFlags(BaseFlags): r"""Wraps up the Discord Channel flags. .. container:: operations .. describe:: x == y Checks if two ChannelFlags are equal. .. describe:: x != y Checks if two ChannelFlags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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. """
[docs] @flag_value def pinned(self): """:class:`bool`: Returns ``True`` if the thread is pinned to the top of its parent forum channel.""" return 1 << 1
[docs] @flag_value def require_tag(self): """:class:`bool`: Returns ``True`` if a tag is required to be specified when creating a thread in a :class:`ForumChannel`. .. versionadded:: 2.2 """ return 1 << 4
[docs]@fill_with_flags() class AttachmentFlags(BaseFlags): r"""Wraps up the Discord Attachment flags. .. container:: operations .. describe:: x == y Checks if two flags are equal. .. describe:: x != y Checks if two flags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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.5 Attributes ----------- value: :class:`int` The raw value. You should query flags via the properties rather than using this raw value. """ __slots__ = ()
[docs] @flag_value def is_clip(self): """:class:`bool`: Returns ``True`` if the attachment is a clip.""" return 1 << 0
[docs] @flag_value def is_thumbnail(self): """:class:`bool`: Returns ``True`` if the attachment is a thumbnail.""" return 1 << 1
[docs] @flag_value def is_remix(self): """:class:`bool`: Returns ``True`` if the attachment has been remixed.""" return 1 << 2
[docs]@fill_with_flags() class SKUFlags(BaseFlags): r"""Wraps up the Discord SKU flags. .. container:: operations .. describe:: x == y Checks if two SKUFlags are equal. .. describe:: x != y Checks if two SKUFlags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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.5 Attributes ----------- value: :class:`int` The raw value. You should query flags via the properties rather than using this raw value. """ __slots__ = ()
[docs] @flag_value def available(self): """:class:`bool`: Returns ``True`` if the SKU is available for purchase.""" return 1 << 2
[docs] @flag_value def guild_subscription(self): """:class:`bool`: Returns ``True`` if the SKU is a guild subscription.""" return 1 << 7
[docs] @flag_value def user_subscription(self): """:class:`bool`: Returns ``True`` if the SKU is a user subscription.""" return 1 << 8
[docs]@fill_with_flags() class MemberFlags(BaseFlags): r"""Wraps up the Discord Member flags. .. container:: operations .. describe:: x == y Checks if two MemberFlags are equal. .. describe:: x != y Checks if two MemberFlags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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.6 Attributes ----------- value: :class:`int` The raw value. You should query flags via the properties rather than using this raw value. """ __slots__ = ()
[docs] @flag_value def did_rejoin(self): """:class:`bool`: Returns ``True`` if the member left and rejoined the guild.""" return 1 << 0
[docs] @flag_value def completed_onboarding(self): """:class:`bool`: Returns ``True`` if the member has completed onboarding.""" return 1 << 1
[docs] @flag_value def bypasses_verification(self): """:class:`bool`: Returns ``True`` if the member is exempt from verification requirements. .. note:: This can be edited through :func:`~discord.Member.edit`. """ return 1 << 2
[docs] @flag_value def started_onboarding(self): """:class:`bool`: Returns ``True`` if the member has started onboarding.""" return 1 << 3
[docs]@fill_with_flags() class RoleFlags(BaseFlags): r"""Wraps up the Discord Role flags. .. container:: operations .. describe:: x == y Checks if two RoleFlags are equal. .. describe:: x != y Checks if two RoleFlags are not equal. .. describe:: x + y Adds two flags together. Equivalent to ``x | y``. .. describe:: x - y Subtracts two flags from each other. .. describe:: x | y Returns the union of two flags. Equivalent to ``x + y``. .. describe:: x & y Returns the intersection of two flags. .. describe:: ~x Returns the inverse of a flag. .. 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.6 Attributes ----------- value: :class:`int` The raw value. This value is a bit array field of a 53-bit integer representing the currently available flags. You should query flags via the properties rather than using this raw value. """ __slots__ = ()
[docs] @flag_value def in_prompt(self): """:class:`bool`: Returns ``True`` if the role is selectable in one of the guild's :class:`~discord.OnboardingPrompt`.""" return 1 << 0