From 0c7a520aa0368b9940ba3b0a2b1003456e25b04d Mon Sep 17 00:00:00 2001 From: Noah Date: Sat, 9 Jul 2022 14:14:06 +0100 Subject: refactor(client): Use REST for `Client.user`, add enums for flags and premium type --- discord/client.py | 14 +++-- discord/flags.py | 35 +++++++++++++ discord/premium_type.py | 8 +++ discord/user.py | 134 ++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 166 insertions(+), 25 deletions(-) create mode 100644 discord/flags.py create mode 100644 discord/premium_type.py diff --git a/discord/client.py b/discord/client.py index 0ac042e..a2716b0 100644 --- a/discord/client.py +++ b/discord/client.py @@ -6,15 +6,23 @@ from typing import Optional, Coroutine, Any, Callable import websockets from .utils import EventEmitter -from .intents import Intents, gen_number +from .utils.rest import get +from .intents import Intents, get_number from .user import User class Client: + _token: str + + @property + async def user(self): + data = await get(self._token, '/users/@me') + return User(data) + def __init__(self, intents: list[Intents]): self.gateway = None self.loop = asyncio.get_event_loop() - self.code = gen_number(intents) + self.code = get_number(intents) self.event_emitter = EventEmitter() async def connect(self, token: str, intent_code: int): @@ -39,7 +47,6 @@ class Client: await gateway.send(json.dumps(identify)) ready = await gateway.recv() self.event_emitter.emit('on_ready') - self.user = User(json.loads(ready)['d']['user']) async def send(self, data: dict): """ @@ -86,4 +93,5 @@ class Client: """ Run the client. """ + self._token = token asyncio.run(self.connect(token, self.code)) diff --git a/discord/flags.py b/discord/flags.py new file mode 100644 index 0000000..65a9253 --- /dev/null +++ b/discord/flags.py @@ -0,0 +1,35 @@ +from enum import Enum, unique + + +@unique +class Flags(Enum): + STAFF = 1 + PARTNER = 2 + HYPESQUAD = 4 + BUG_HUNTER_LEVEL_1 = 8 + HYPESQUAD_ONLINE_HOUSE_1 = 64 + HYPESQUAD_ONLINE_HOUSE_2 = 128 + HYPESQUAD_ONLINE_HOUSE_3 = 256 + TEAM_PSUEDO_USER = 1024 + BUG_HUNTER_LEVEL_2 = 16384 + VERIFIED_BOT = 65536 + VERIFIED_DEVELOPER = 131072 + CERTIFIED_MODERATOR = 262144 + BOT_HTTP_INTERACTIONS = 524288 + + +def get_number(flags: list[Flags]): + number = 1 + for i in flags: + number += i.value + return number + +def get_flags(number: int): + flags = [] + while number != 0: + for i in Flags: + if number >= i.value: + flags.append(i) + number -= i.value + return flags + diff --git a/discord/premium_type.py b/discord/premium_type.py new file mode 100644 index 0000000..3549f92 --- /dev/null +++ b/discord/premium_type.py @@ -0,0 +1,8 @@ +from enum import Enum, unique + + +@unique +class PremiumType(Enum): + NONE = 0, + NITRO_CLASSIC = 1, + NITRO = 2 \ No newline at end of file diff --git a/discord/user.py b/discord/user.py index 6000b31..ff86c26 100644 --- a/discord/user.py +++ b/discord/user.py @@ -1,26 +1,116 @@ +from discord.flags import get_flags, Flags +from discord.premium_type import PremiumType + + class User: - __slots__ = ( - "id", - "username", - "discriminator", - "avatar", - "avatar_decoration" - "bot", - "system", - "mfa_enabled", - "banner", - "banner_color", - "accent_color", - "bio", - "pronouns", - "locale", - "verified", - "email", - "flags", - "premium_type", - "public_flags" - ) + _id: int + _username: str + _discriminator: str + _avatar: str + _bot: bool + _system: bool + _mfa_enabled: bool + _banner: str + _accent_color: int + _locale: str + _verified: bool + _email: str + _pronouns: str + _bio: str + _flags: list[Flags] + _premium_type: PremiumType + _public_flags: list[Flags] + + @property + def id(self): + """The user's ID.""" + return self._id + + @property + def username(self): + """The user's username. This is not unique.""" + return self._username + + @property + def discriminator(self): + """The user's 4-digit tag.""" + return self._discriminator + + @property + def avatar(self): + """The user's avatar hash.""" + return self._avatar + + @property + def bot(self): + """Whether the user is a bot.""" + return self._bot + + @property + def pronouns(self): + """The user's pronouns (not yet implemented into Discord frontend).""" + return self._pronouns + + @property + def bio(self): + """The contents of the user's About Me section.""" + return self._bio + + @property + def system(self): + """Whether the user is an Official Discord System user (for urgent messages).""" + return self._system + + @property + def mfa_enabled(self): + """Whether the user has 2FA set up.""" + return self._mfa_enabled + + @property + def banner(self): + """The user's banner hash.""" + return self._banner + + @property + def accent_color(self): + """The user's banner color.""" + return self._accent_color + + @property + def locale(self): + """The user's chosen language.""" + return self._locale + + @property + def verified(self): + """Whether the email on the user's account is verified.""" + return self._verified + + @property + def email(self): + """The user's email.""" + return self._email + + @property + def flags(self): + """The flags on the user's account.""" + return self._flags + + @property + def premium_type(self): + """The type of Nitro subscription on a user's account.""" + return self._premium_type + + @property + def public_flags(self): + """The public flags on a user's account.""" + return self._public_flags def __init__(self, data: dict): for k in data: - setattr(self, k, data[k]) + if k == "flags" or k == "public_flags": + setattr(self, f"_{k}", get_flags(data[k])) + elif k == "premium_type": + setattr(self, f"_{k}", PremiumType(data[k])) + else: + setattr(self, f"_{k}", data[k]) -- cgit 1.4.1-2-gfad0