From ab5f995d78c12005cae7cf51c174656207e0e134 Mon Sep 17 00:00:00 2001 From: JohnyTheCarrot Date: Sun, 12 Apr 2020 21:08:39 +0200 Subject: [PATCH] Add support for public user flags --- discord/enums.py | 3 ++ discord/flags.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++ discord/member.py | 2 +- discord/user.py | 9 ++++- docs/api.rst | 6 +++ 5 files changed, 115 insertions(+), 2 deletions(-) diff --git a/discord/enums.py b/discord/enums.py index 8feb13bb4..6859a1c75 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -400,6 +400,9 @@ class UserFlags(Enum): early_supporter = 512 team_user = 1024 system = 4096 + bug_hunter_level_2 = 16384 + verified_bot = 65536 + verified_bot_developer = 131072 class ActivityType(Enum): unknown = -1 diff --git a/discord/flags.py b/discord/flags.py index ff0c10c12..448bced2f 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -24,9 +24,12 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from .enums import UserFlags + __all__ = ( 'SystemChannelFlags', 'MessageFlags', + 'PublicUserFlags' ) class flag_value: @@ -230,3 +233,97 @@ class MessageFlags(BaseFlags): An urgent message is one sent by Discord Trust and Safety. """ return 16 + +@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. + + .. 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__ = () + + @flag_value + def staff(self): + """:class:`bool`: Returns ``True`` if the user is a Discord Employee.""" + return UserFlags.staff.value + + @flag_value + def partner(self): + """:class:`bool`: Returns ``True`` if the user is a Discord Partner.""" + return UserFlags.partner.value + + @flag_value + def hypesquad(self): + """:class:`bool`: Returns ``True`` if the user is a HypeSquad Events member.""" + return UserFlags.hypesquad.value + + @flag_value + def bug_hunter(self): + """:class:`bool`: Returns ``True`` if the user is a Bug Hunter""" + return UserFlags.bug_hunter.value + + @flag_value + def hypesquad_bravery(self): + """:class:`bool`: Returns ``True`` if the user is a HypeSquad Bravery member.""" + return UserFlags.hypesquad_bravery.value + + @flag_value + def hypesquad_brilliance(self): + """:class:`bool`: Returns ``True`` if the user is a HypeSquad Brilliance member.""" + return UserFlags.hypesquad_brilliance.value + + @flag_value + def hypesquad_balance(self): + """:class:`bool`: Returns ``True`` if the user is a HypeSquad Balance member.""" + return UserFlags.hypesquad_balance.value + + @flag_value + def early_supporter(self): + """:class:`bool`: Returns ``True`` if the user is an Early Supporter.""" + return UserFlags.early_supporter.value + + @flag_value + def team_user(self): + """:class:`bool`: Returns ``True`` if the user is a Team User.""" + return UserFlags.team_user.value + + @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 + + @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 + + @flag_value + def verified_bot(self): + """:class:`bool`: Returns ``True`` if the user is a Verified Bot.""" + return UserFlags.verified_bot.value + + @flag_value + def verified_bot_developer(self): + """:class:`bool`: Returns ``True`` if the user is a Verified Bot Developer.""" + return UserFlags.verified_bot_developer.value + + def all(self): + """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)] diff --git a/discord/member.py b/discord/member.py index 22c4db6ac..e25e92e83 100644 --- a/discord/member.py +++ b/discord/member.py @@ -33,7 +33,7 @@ from . import utils from .user import BaseUser, User from .activity import create_activity from .permissions import Permissions -from .enums import Status, try_enum +from .enums import Status, try_enum, UserFlags, HypeSquadHouse from .colour import Colour from .object import Object diff --git a/discord/user.py b/discord/user.py index 77c0b4739..69947a607 100644 --- a/discord/user.py +++ b/discord/user.py @@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE. from collections import namedtuple import discord.abc +from .flags import PublicUserFlags from .utils import snowflake_time, _bytes_to_base64_data, parse_time from .enums import DefaultAvatar, RelationshipType, UserFlags, HypeSquadHouse, PremiumType, try_enum from .errors import ClientException @@ -82,7 +83,7 @@ class Profile(namedtuple('Profile', 'flags user mutual_guilds connected_accounts _BaseUser = discord.abc.User class BaseUser(_BaseUser): - __slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', 'system', '_state') + __slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', 'system', '_public_flags', '_state') def __init__(self, *, state, data): self._state = state @@ -105,6 +106,7 @@ class BaseUser(_BaseUser): self.id = int(data['id']) self.discriminator = data['discriminator'] self.avatar = data['avatar'] + self._public_flags = data.get('public_flags', 0) self.bot = data.get('bot', False) self.system = data.get('system', False) @@ -130,6 +132,11 @@ class BaseUser(_BaseUser): 'bot': self.bot, } + @property + def public_flags(self): + """:class:`PublicFlags`: The publicly available flags the user has.""" + return PublicUserFlags._from_value(self._public_flags) + @property def avatar_url(self): """Returns an :class:`Asset` for the avatar the user has. diff --git a/docs/api.rst b/docs/api.rst index 6a1b66141..72ad9ae08 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -2647,6 +2647,12 @@ MessageFlags .. autoclass:: MessageFlags :members: +PublicUserFlags +~~~~~~~~~~~~~~~ + +.. autoclass:: PublicUserFlags + :members: + Exceptions ------------