Skip to content

Commit f0700e0

Browse files
authored
Added support for Display Name Styles (#860)
1 parent 4436b2d commit f0700e0

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

discord/enums.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
'AuditLogAction',
4141
'AuditLogActionCategory',
4242
'UserFlags',
43+
'NameFont',
44+
'NameEffect',
4345
'ActivityType',
4446
'NotificationLevel',
4547
'HighlightLevel',
@@ -694,6 +696,27 @@ class UserFlags(Enum):
694696
collaborator = 1125899906842624
695697
restricted_collaborator = 2251799813685248
696698

699+
class NameFont(Enum):
700+
default = 11
701+
bangers = 1
702+
bio_rhyme = 2
703+
cherry_bomb = 3
704+
chicle = 4
705+
compagnon = 5
706+
museo_moderno = 6
707+
neo_castel = 7
708+
pixelify = 8
709+
ribes = 9
710+
sinistre = 10
711+
zilla_slab = 12
712+
713+
class NameEffect(Enum):
714+
solid = 1
715+
gradient = 2
716+
neon = 3
717+
toon = 4
718+
pop = 5
719+
glow = 6
697720

698721
class ActivityType(Enum):
699722
unknown = -1

discord/types/user.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class PartialUser(TypedDict):
4242
system: NotRequired[bool]
4343
global_name: Optional[str]
4444
primary_guild: NotRequired[Optional[PrimaryGuild]]
45+
display_name_styles: Optional[DisplayNameStyle]
4546

4647

4748
ConnectionType = Literal[
@@ -70,6 +71,8 @@ class PartialUser(TypedDict):
7071
]
7172
ConnectionVisibilty = Literal[0, 1]
7273
PremiumType = Literal[0, 1, 2, 3]
74+
DisplayNameFont = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
75+
DisplayNameEffect = Literal[1, 2, 3, 4, 5, 6]
7376

7477

7578
class APIUser(PartialUser):
@@ -96,6 +99,12 @@ class User(APIUser, total=False):
9699
mobile: bool
97100

98101

102+
class DisplayNameStyle(TypedDict):
103+
font_id: DisplayNameFont
104+
effect_id: DisplayNameEffect
105+
colors: List[int] # 1-2
106+
107+
99108
class UserWithToken(User):
100109
token: str
101110

discord/user.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
PremiumType,
3636
RelationshipAction,
3737
RelationshipType,
38+
NameEffect,
39+
NameFont,
3840
try_enum,
3941
)
4042
from .errors import NotFound
@@ -73,6 +75,7 @@
7375
UserAvatar as UserAvatarPayload,
7476
AvatarDecorationData,
7577
PrimaryGuild as PrimaryGuildPayload,
78+
DisplayNameStyle as DisplayNameStylePayload
7679
)
7780
from .types.snowflake import Snowflake
7881

@@ -81,13 +84,35 @@
8184
'User',
8285
'ClientUser',
8386
'RecentAvatar',
87+
'DisplayNameStyle',
8488
)
8589

8690

8791
class _UserTag:
8892
__slots__ = ()
8993
id: int
9094

95+
class DisplayNameStyle:
96+
"""Represents a user's display name style.
97+
98+
.. versionadded:: 2.1
99+
100+
Attributes
101+
-----------
102+
font: :class:`NameFont`
103+
The font used for the display name.
104+
effect: :class:`NameEffect`
105+
The visual effect applied to the display name.
106+
colors: List[:class:`Colour`]
107+
The list of colours applied to the display name.
108+
"""
109+
def __init__(self, *, data: DisplayNameStylePayload) -> None:
110+
self.font: NameFont = try_enum(NameFont, data['font_id'])
111+
self.effect: NameEffect = try_enum(NameEffect, data['effect_id'])
112+
self.colors: List[discord.Colour] = [discord.Colour(color) for color in data.get('colors', [])]
113+
114+
def __repr__(self) -> str:
115+
return f'<DisplayNameStyle font={self.font} effect={self.effect} colors={self.colors}>'
91116

92117
class BaseUser(_UserTag):
93118
__slots__ = (
@@ -105,6 +130,7 @@ class BaseUser(_UserTag):
105130
'premium_type',
106131
'_state',
107132
'_primary_guild',
133+
'_display_name_style',
108134
)
109135

110136
if TYPE_CHECKING:
@@ -121,6 +147,7 @@ class BaseUser(_UserTag):
121147
_accent_colour: Optional[int]
122148
_public_flags: int
123149
_primary_guild: Optional[PrimaryGuildPayload]
150+
_display_name_style: Optional[DisplayNameStylePayload]
124151

125152
def __init__(self, *, state: ConnectionState, data: Union[UserPayload, PartialUserPayload]) -> None:
126153
self._state = state
@@ -159,6 +186,7 @@ def _update(self, data: Union[UserPayload, PartialUserPayload]) -> None:
159186
self.bot = data.get('bot', False)
160187
self.system = data.get('system', False)
161188
self._primary_guild = data.get('primary_guild', None)
189+
self._display_name_style = data.get('display_name_styles', None) or None
162190

163191
@classmethod
164192
def _copy(cls, user: Self) -> Self:
@@ -177,6 +205,7 @@ def _copy(cls, user: Self) -> Self:
177205
self.system = user.system
178206
self._state = user._state
179207
self._primary_guild = user._primary_guild
208+
self._display_name_style = user._display_name_style
180209

181210
return self
182211

@@ -194,6 +223,7 @@ def _to_minimal_user_json(self) -> APIUserPayload:
194223
'banner': self._banner,
195224
'accent_color': self._accent_colour,
196225
'primary_guild': self._primary_guild,
226+
'display_name_styles': self._display_name_style,
197227
}
198228
return user
199229

@@ -383,6 +413,16 @@ def primary_guild(self) -> PrimaryGuild:
383413
return PrimaryGuild(state=self._state, data=self._primary_guild)
384414
return PrimaryGuild._default(self._state)
385415

416+
@property
417+
def display_name_style(self) -> Optional[DisplayNameStyle]:
418+
""":class:`DisplayNameStyle`: Returns the user's display name style.
419+
420+
.. versionadded:: 2.1
421+
"""
422+
if self._display_name_style is None:
423+
return None
424+
return DisplayNameStyle(data=self._display_name_style)
425+
386426
def mentioned_in(self, message: Message) -> bool:
387427
"""Checks if the user is mentioned in the specified message.
388428
@@ -1021,6 +1061,7 @@ def _update_self(self, user: Union[PartialUserPayload, Tuple[()]]) -> Optional[T
10211061
self._avatar_decoration_data,
10221062
self.global_name,
10231063
self._primary_guild,
1064+
self._display_name_style,
10241065
)
10251066
modified = (
10261067
user['username'],
@@ -1030,6 +1071,7 @@ def _update_self(self, user: Union[PartialUserPayload, Tuple[()]]) -> Optional[T
10301071
user.get('avatar_decoration_data'),
10311072
user.get('global_name'),
10321073
user.get('primary_guild'),
1074+
user.get('display_name_styles'),
10331075
)
10341076
if original != modified:
10351077
to_return = User._copy(self)
@@ -1041,6 +1083,7 @@ def _update_self(self, user: Union[PartialUserPayload, Tuple[()]]) -> Optional[T
10411083
self._avatar_decoration_data,
10421084
self.global_name,
10431085
self._primary_guild,
1086+
self._display_name_style,
10441087
) = modified
10451088
# Signal to dispatch user_update
10461089
return to_return, self

docs/api.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,86 @@ of :class:`enum.Enum`.
21792179

21802180
.. versionadded:: 2.1
21812181

2182+
.. class:: NameFont
2183+
2184+
Specifies the font of :class:`DisplayNameStyle`.
2185+
2186+
.. attribute:: default
2187+
2188+
The default font.
2189+
2190+
.. attribute:: bangers
2191+
2192+
"Bangers" font.
2193+
2194+
.. attribute:: bio_rhyme
2195+
2196+
"BioRhyme" font.
2197+
2198+
.. attribute:: cherry_bomb
2199+
2200+
"Cherry Bomb One" font.
2201+
2202+
.. attribute:: chicle
2203+
2204+
"Chicle" font.
2205+
2206+
.. attribute:: compagnon
2207+
2208+
"Compagnon" font.
2209+
2210+
.. attribute:: museo_moderno
2211+
2212+
"MuseoModerno" font.
2213+
2214+
.. attribute:: neo_castel
2215+
2216+
"Néo-Castel" font.
2217+
2218+
.. attribute:: pixelify
2219+
2220+
"Pixelify Sans" font.
2221+
2222+
.. attribute:: ribes
2223+
2224+
"Ribes" font.
2225+
2226+
.. attribute:: sinistre
2227+
2228+
"Sinistre" font.
2229+
2230+
.. attribute:: zilla_slab
2231+
2232+
"Zilla Slab" font.
2233+
2234+
.. class:: NameEffect
2235+
2236+
Specifies the effect of :class:`DisplayNameStyle`.
2237+
2238+
.. attribute:: solid
2239+
2240+
Displays the first color provided.
2241+
2242+
.. attribute:: gradient
2243+
2244+
Two color gradient.
2245+
2246+
.. attribute:: neon
2247+
2248+
Glow around the name.
2249+
2250+
.. attribute:: toon
2251+
2252+
Subtle vertical gradient and stroke.
2253+
2254+
.. attribute:: pop
2255+
2256+
Colored dropshadow.
2257+
2258+
.. attribute:: glow
2259+
2260+
Alternate gradient style.
2261+
21822262
.. class:: ActivityType
21832263

21842264
Specifies the type of :class:`Activity`. This is used to check how to
@@ -7301,6 +7381,11 @@ User
73017381
.. autoclass:: PrimaryGuild()
73027382
:members:
73037383

7384+
.. attributetable:: DisplayNameStyle
7385+
7386+
.. autoclass:: DisplayNameStyle()
7387+
:members:
7388+
73047389
Affinity
73057390
~~~~~~~~~
73067391

0 commit comments

Comments
 (0)