1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
2
|
|
|
# Full MIT License can be found in `LICENSE` at the project root. |
3
|
|
|
|
4
|
|
|
from __future__ import annotations |
5
|
|
|
|
6
|
|
|
from enum import IntEnum |
7
|
|
|
from typing import TYPE_CHECKING |
8
|
|
|
from dataclasses import dataclass |
9
|
|
|
|
10
|
|
|
from ...utils.types import MISSING |
11
|
|
|
from ...utils.api_object import APIObject |
|
|
|
|
12
|
|
|
|
13
|
|
|
import io |
|
|
|
|
14
|
|
|
from aiohttp import ClientSession |
|
|
|
|
15
|
|
|
|
16
|
|
|
PILLOW_IMPORT = True |
17
|
|
|
|
18
|
|
|
try: |
19
|
|
|
from PIL import Image |
20
|
|
|
except (ModuleNotFoundError, ImportError): |
21
|
|
|
PILLOW_IMPORT = False |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
if TYPE_CHECKING: |
25
|
|
|
from typing import Optional |
|
|
|
|
26
|
|
|
|
27
|
|
|
from ...client import Client |
|
|
|
|
28
|
|
|
from ...utils.types import APINullable |
29
|
|
|
from ...utils.snowflake import Snowflake |
30
|
|
|
from ...utils.conversion import convert, construct_client_dict |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class PremiumTypes(IntEnum): |
34
|
|
|
"""The type of Discord premium a user has. |
35
|
|
|
|
36
|
|
|
Attributes |
37
|
|
|
---------- |
38
|
|
|
NONE: |
39
|
|
|
No nitro. |
40
|
|
|
NITRO_CLASSIC: |
41
|
|
|
Nitro classic (no boosts). |
42
|
|
|
NITRO: |
43
|
|
|
Full nitro subscription. |
44
|
|
|
""" |
45
|
|
|
NONE = 0 |
46
|
|
|
NITRO_CLASSIC = 1 |
47
|
|
|
NITRO = 2 |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class VisibilityType(IntEnum): |
51
|
|
|
"""The type of a connection visibility. |
52
|
|
|
|
53
|
|
|
Attributes |
54
|
|
|
---------- |
55
|
|
|
NONE: |
56
|
|
|
Connection is not visible. |
57
|
|
|
EVERYONE: |
58
|
|
|
Connection is visible to everyone. |
59
|
|
|
""" |
60
|
|
|
NONE = 0 |
61
|
|
|
EVERYONE = 1 |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
@dataclass |
|
|
|
|
65
|
|
|
class User(APIObject): |
66
|
|
|
"""Represents a Discord user. This can be a bot account or a |
67
|
|
|
human account. |
68
|
|
|
|
69
|
|
|
Attributes |
70
|
|
|
---------- |
71
|
|
|
avatar: Optional[:class:`str`] |
72
|
|
|
The user's avatar hash |
73
|
|
|
discriminator: :class:`str` |
74
|
|
|
The user's 4-digit discord-tag |
75
|
|
|
id: :class:`~pincer.utils.snowflake.Snowflake` |
76
|
|
|
The user's id |
77
|
|
|
username: :class:`str` |
78
|
|
|
The user's username, not unique across the platform |
79
|
|
|
flags: APINullable[:class:`int`] |
80
|
|
|
The flags on a user's account |
81
|
|
|
accent_color: APINullable[Optional[:class:`int`]] |
82
|
|
|
The user's banner color encoded as an integer representation of |
83
|
|
|
hexadecimal color code |
84
|
|
|
banner: APINullable[Optional[:class:`str`]] |
85
|
|
|
The user's banner, or null if unset |
86
|
|
|
banner_color: APINullable[Optional[:class:`int`]] |
87
|
|
|
The color of the user's banner |
88
|
|
|
bot: APINullable[:class:`bool`] |
89
|
|
|
Whether the user belongs to an OAuth2 application |
90
|
|
|
email: APINullable[Optional[:class:`str`]] |
91
|
|
|
The user's email |
92
|
|
|
locale: APINullable[:class:`str`] |
93
|
|
|
The user's chosen language option |
94
|
|
|
mfa_enabled: APINullable[:class:`bool`] |
95
|
|
|
Whether the user has two factor enabled on their account |
96
|
|
|
premium_type: APINullable[:class:`int`] |
97
|
|
|
The type of Nitro subscription on a user's account |
98
|
|
|
public_flags: APINullable[:class:`int`] |
99
|
|
|
The public flags on a user's account |
100
|
|
|
system: APINullable[:class:`bool`] |
101
|
|
|
Whether the user is an Official Discord System user |
102
|
|
|
(part of the urgent message system) |
103
|
|
|
verified: APINullable[:class:`bool`] |
104
|
|
|
Whether the email on this account has been verified |
105
|
|
|
""" |
106
|
|
|
discriminator: str |
107
|
|
|
id: Snowflake |
108
|
|
|
username: str |
109
|
|
|
|
110
|
|
|
avatar: APINullable[str] = MISSING |
111
|
|
|
flags: APINullable[int] = MISSING |
112
|
|
|
accent_color: APINullable[Optional[int]] = MISSING |
113
|
|
|
banner: APINullable[Optional[str]] = MISSING |
114
|
|
|
banner_color: APINullable[Optional[int]] = MISSING |
115
|
|
|
bot: APINullable[bool] = MISSING |
116
|
|
|
email: APINullable[Optional[str]] = MISSING |
117
|
|
|
locale: APINullable[str] = MISSING |
118
|
|
|
mfa_enabled: APINullable[bool] = MISSING |
119
|
|
|
premium_type: APINullable[int] = MISSING |
120
|
|
|
public_flags: APINullable[int] = MISSING |
121
|
|
|
system: APINullable[bool] = MISSING |
122
|
|
|
verified: APINullable[bool] = MISSING |
123
|
|
|
|
124
|
|
|
@property |
125
|
|
|
def premium(self) -> APINullable[PremiumTypes]: |
126
|
|
|
"""APINullable[:class:`~pincer.objects.user.user.PremiumTypes`]: The |
127
|
|
|
user their premium type in a usable enum. |
128
|
|
|
""" |
129
|
|
|
return ( |
130
|
|
|
MISSING |
131
|
|
|
if self.premium_type is MISSING |
132
|
|
|
else PremiumTypes(self.premium_type) |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
@property |
136
|
|
|
def mention(self) -> str: |
137
|
|
|
""":class:`str`: The user's mention string.""" |
138
|
|
|
return f"<@!{self.id}>" |
139
|
|
|
|
140
|
|
|
def get_avatar_url(self, size: int = 512, ext: str = 'png') -> str: |
|
|
|
|
141
|
|
|
return ( |
142
|
|
|
f"https://cdn.discordapp.com/avatars/{self.id}/{self.avatar}.{ext}" |
143
|
|
|
f"?size={size}" |
144
|
|
|
) |
145
|
|
|
|
146
|
|
|
if PILLOW_IMPORT: |
147
|
|
|
async def get_avatar(self, size=512, ext='png') -> Image: |
|
|
|
|
148
|
|
|
async with ClientSession().get(url=self.get_avatar_url()) as resp: |
149
|
|
|
avatar = io.BytesIO(await resp.read()) |
150
|
|
|
print(Image, dir(Image)) |
151
|
|
|
return Image.open(avatar).convert("RGBA") |
152
|
|
|
|
153
|
|
|
def __str__(self): |
154
|
|
|
return self.username + '#' + self.discriminator |
155
|
|
|
|
156
|
|
|
@classmethod |
157
|
|
|
async def from_id(cls, client: Client, user_id: int) -> User: |
|
|
|
|
158
|
|
|
data = await client.http.get(f"users/{user_id}") |
159
|
|
|
return cls.from_dict(construct_client_dict(client, data)) |
|
|
|
|
160
|
|
|
|