Browse Source

Add alias to primary, secondary and tertiary color

pull/10214/head
makerze 4 weeks ago
parent
commit
f1047a59de
  1. 65
      discord/guild.py
  2. 107
      discord/role.py

65
discord/guild.py

@ -3648,9 +3648,9 @@ class Guild(Hashable):
hoist: bool = ..., hoist: bool = ...,
display_icon: Union[bytes, str] = MISSING, display_icon: Union[bytes, str] = MISSING,
mentionable: bool = ..., mentionable: bool = ...,
primary_color: Union[Colour, int, None] = ..., primary_colour: Union[Colour, int, None] = ...,
secondary_color: Union[Colour, int, None] = ..., secondary_colour: Union[Colour, int, None] = ...,
tertiary_color: Union[Colour, int, None] = ..., tertiary_colour: Union[Colour, int, None] = ...,
) -> Role: ) -> Role:
... ...
@ -3685,6 +3685,9 @@ class Guild(Hashable):
primary_color: Union[Colour, int, None] = MISSING, primary_color: Union[Colour, int, None] = MISSING,
secondary_color: Union[Colour, int, None] = MISSING, secondary_color: Union[Colour, int, None] = MISSING,
tertiary_color: Union[Colour, int, None] = MISSING, tertiary_color: Union[Colour, int, None] = MISSING,
primary_colour: Union[Colour, int, None] = MISSING,
secondary_colour: Union[Colour, int, None] = MISSING,
tertiary_colour: Union[Colour, int, None] = MISSING,
) -> Role: ) -> Role:
"""|coro| """|coro|
@ -3713,12 +3716,12 @@ class Guild(Hashable):
colour: Union[:class:`Colour`, :class:`int`] colour: Union[:class:`Colour`, :class:`int`]
The colour for the role. Defaults to :meth:`Colour.default`. The colour for the role. Defaults to :meth:`Colour.default`.
This is aliased to ``color`` as well. This is aliased to ``color`` as well.
primary_color: Union[:class:`Colour`, :class:`int`, None] primary_colour: Union[:class:`Colour`, :class:`int`, None]
The primary color for the role. If provided, must be an integer or :class:`Colour`. The primary colour for the role. If provided, must be an integer or :class:`Colour`.
secondary_color: Union[:class:`Colour`, :class:`int`, None] secondary_colour: Union[:class:`Colour`, :class:`int`, None]
The secondary color for the role. Requires ``primary_color`` to also be set. The secondary colour for the role.
tertiary_color: Union[:class:`Colour`, :class:`int`, None] tertiary_colour: Union[:class:`Colour`, :class:`int`, None]
The tertiary color for the role. Can only be used for the holographic role preset, The tertiary colour for the role. Can only be used for the holographic role preset,
which is ``(11127295, 16759788, 16761760)`` which is ``(11127295, 16759788, 16761760)``
hoist: :class:`bool` hoist: :class:`bool`
Indicates if the role should be shown separately in the member list. Indicates if the role should be shown separately in the member list.
@ -3761,34 +3764,44 @@ class Guild(Hashable):
fields['color'] = actual_colour.value fields['color'] = actual_colour.value
solid_color_used = color is not MISSING or colour is not MISSING solid_color_used = color is not MISSING or colour is not MISSING
colors_used = primary_color is not MISSING or secondary_color is not MISSING or tertiary_color is not MISSING colors_used = (
primary_color is not MISSING
or secondary_color is not MISSING
or tertiary_color is not MISSING
or primary_colour is not MISSING
or secondary_colour is not MISSING
or tertiary_colour is not MISSING
)
if solid_color_used and colors_used: if solid_color_used and colors_used:
raise TypeError( raise TypeError(
"You must choose either only solid color (color/colour) or colors (primary_color/secondary_color/tertiary_color), not both." "You must choose either only solid colour (color/colour) or colours (primary_colour/secondary_colour/tertiary_colour), not both."
) )
actual_primary_colour = primary_colour or primary_color
actual_secondary_colour = secondary_colour or secondary_color
actual_tertiary_colour = tertiary_colour or tertiary_color
colors_payload: Dict[str, Any] = {} colors_payload: Dict[str, Any] = {}
if primary_color is not MISSING: if actual_primary_colour is not MISSING:
if primary_color is None: if actual_primary_colour is None:
colors_payload['primary_color'] = None colors_payload['primary_color'] = None
elif isinstance(primary_color, int): elif isinstance(actual_primary_colour, int):
colors_payload['primary_color'] = primary_color colors_payload['primary_color'] = actual_primary_colour
else: else:
colors_payload['primary_color'] = primary_color.value colors_payload['primary_color'] = actual_primary_colour.value
if secondary_color is not MISSING: if actual_secondary_colour is not MISSING:
if secondary_color is None: if actual_secondary_colour is None:
colors_payload['secondary_color'] = None colors_payload['secondary_color'] = None
elif isinstance(secondary_color, int): elif isinstance(actual_secondary_colour, int):
colors_payload['secondary_color'] = secondary_color colors_payload['secondary_color'] = actual_secondary_colour
else: else:
colors_payload['secondary_color'] = secondary_color.value colors_payload['secondary_color'] = actual_secondary_colour.value
if tertiary_color is not MISSING: if actual_tertiary_colour is not MISSING:
if tertiary_color is None: if actual_tertiary_colour is None:
colors_payload['tertiary_color'] = None colors_payload['tertiary_color'] = None
elif isinstance(tertiary_color, int): elif isinstance(actual_tertiary_colour, int):
colors_payload['tertiary_color'] = tertiary_color colors_payload['tertiary_color'] = actual_tertiary_colour
else: else:
colors_payload['tertiary_color'] = tertiary_color.value colors_payload['tertiary_color'] = actual_tertiary_colour.value
if colors_payload: if colors_payload:
fields['colors'] = colors_payload fields['colors'] = colors_payload

107
discord/role.py

@ -222,9 +222,9 @@ class Role(Hashable):
'tags', 'tags',
'_flags', '_flags',
'_state', '_state',
'_primary_color', '_primary_colour',
'_secondary_color', '_secondary_colour',
'_tertiary_color', '_tertiary_colour',
) )
def __init__(self, *, guild: Guild, state: ConnectionState, data: RolePayload): def __init__(self, *, guild: Guild, state: ConnectionState, data: RolePayload):
@ -288,9 +288,9 @@ class Role(Hashable):
self.tags: Optional[RoleTags] self.tags: Optional[RoleTags]
self._flags: int = data.get('flags', 0) self._flags: int = data.get('flags', 0)
colors = data.get('colors', {}) colors = data.get('colors', {})
self._primary_color = colors.get('primary_color', None) self._primary_colour = colors.get('primary_colour', None)
self._secondary_color = colors.get('secondary_color', None) self._secondary_colour = colors.get('secondary_colour', None)
self._tertiary_color = colors.get('tertiary_color', None) self._tertiary_colour = colors.get('tertiary_colour', None)
try: try:
self.tags = RoleTags(data['tags']) # pyright: ignore[reportTypedDictNotRequiredAccess] self.tags = RoleTags(data['tags']) # pyright: ignore[reportTypedDictNotRequiredAccess]
@ -330,20 +330,35 @@ class Role(Hashable):
me = self.guild.me me = self.guild.me
return not self.is_default() and not self.managed and (me.top_role > self or me.id == self.guild.owner_id) return not self.is_default() and not self.managed and (me.top_role > self or me.id == self.guild.owner_id)
@property
def primary_colour(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's primary colour."""
return Colour(self._primary_colour) if self._primary_colour is not None else None
@property @property
def primary_color(self) -> Optional[Colour]: def primary_color(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's primary color.""" """Optional[:class:`Colour`]: Alias for :attr:`primary_colour`."""
return Colour(self._primary_color) if self._primary_color is not None else None return self.primary_colour
@property
def secondary_colour(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's secondary colour."""
return Colour(self._secondary_colour) if self._secondary_colour is not None else None
@property @property
def secondary_color(self) -> Optional[Colour]: def secondary_color(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's secondary color.""" """Optional[:class:`Colour`]: Alias for :attr:`secondary_colour`."""
return Colour(self._secondary_color) if self._secondary_color is not None else None return self.secondary_colour
@property
def tertiary_colour(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's tertiary colour."""
return Colour(self._tertiary_colour) if self._tertiary_colour is not None else None
@property @property
def tertiary_color(self) -> Optional[Colour]: def tertiary_color(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's tertiary color.""" """Optional[:class:`Colour`]: Alias for :attr:`tertiary_colour`."""
return Colour(self._tertiary_color) if self._tertiary_color is not None else None return self.tertiary_colour
@property @property
def permissions(self) -> Permissions: def permissions(self) -> Permissions:
@ -450,6 +465,9 @@ class Role(Hashable):
primary_color: Union[Colour, int, None] = MISSING, primary_color: Union[Colour, int, None] = MISSING,
secondary_color: Union[Colour, int, None] = MISSING, secondary_color: Union[Colour, int, None] = MISSING,
tertiary_color: Union[Colour, int, None] = MISSING, tertiary_color: Union[Colour, int, None] = MISSING,
primary_colour: Union[Colour, int, None] = MISSING,
secondary_colour: Union[Colour, int, None] = MISSING,
tertiary_colour: Union[Colour, int, None] = MISSING,
) -> Optional[Role]: ) -> Optional[Role]:
"""|coro| """|coro|
@ -480,6 +498,13 @@ class Role(Hashable):
The new permissions to change to. The new permissions to change to.
colour: Union[:class:`Colour`, :class:`int`] colour: Union[:class:`Colour`, :class:`int`]
The new colour to change to. (aliased to color as well) The new colour to change to. (aliased to color as well)
primary_colour: Union[:class:`Colour`, :class:`int`, None]
The new primary colour for the role. If provided, must be an integer or :class:`Colour`.
secondary_colour: Union[:class:`Colour`, :class:`int`, None]
The new secondary colour for the role.
tertiary_colour: Union[:class:`Colour`, :class:`int`, None]
The new tertiary colour for the role. Can only be used for the holographic role preset,
which is ``(11127295, 16759788, 16761760)``
hoist: :class:`bool` hoist: :class:`bool`
Indicates if the role should be shown separately in the member list. Indicates if the role should be shown separately in the member list.
display_icon: Optional[Union[:class:`bytes`, :class:`str`]] display_icon: Optional[Union[:class:`bytes`, :class:`str`]]
@ -495,14 +520,6 @@ class Role(Hashable):
position or it will fail. position or it will fail.
reason: Optional[:class:`str`] reason: Optional[:class:`str`]
The reason for editing this role. Shows up on the audit log. The reason for editing this role. Shows up on the audit log.
primary_color: Union[:class:`Colour`, :class:`int`, None]
The primary color for the role. If provided, must be an integer or :class:`Colour`.
secondary_color: Union[:class:`Colour`, :class:`int`, None]
The secondary color for the role.
tertiary_color: Union[:class:`Colour`, :class:`int`, None]
The tertiary_color color for the role. Used for holographic role.
The holographic preset is:
{"primary_color": 11127295, "secondary_color": 16759788, "tertiary_color": 16761760}
Raises Raises
------- -------
@ -553,34 +570,50 @@ class Role(Hashable):
payload['mentionable'] = mentionable payload['mentionable'] = mentionable
solid_color_used = color is not MISSING or colour is not MISSING solid_color_used = color is not MISSING or colour is not MISSING
colors_used = primary_color is not MISSING or secondary_color is not MISSING or tertiary_color is not MISSING colors_used = (
primary_color is not MISSING
or secondary_color is not MISSING
or tertiary_color is not MISSING
or primary_colour is not MISSING
or secondary_colour is not MISSING
or tertiary_colour is not MISSING
)
if solid_color_used and colors_used: if solid_color_used and colors_used:
raise TypeError( raise TypeError(
"You must choose either only solid color (color/colour) or colors (primary_color/secondary_color/tertiary_color), not both." "You must choose either only solid colour (color/colour) or colours (primary_colour/secondary_colour/tertiary_colour), not both."
) )
colors_payload: Dict[str, Any] = {}
if primary_color is not MISSING: if primary_color is not MISSING:
if primary_color is None: primary_colour = primary_color
if secondary_color is not MISSING:
secondary_colour = secondary_color
if tertiary_color is not MISSING:
tertiary_colour = tertiary_color
colors_payload: Dict[str, Any] = {}
if primary_colour is not MISSING:
if primary_colour is None:
colors_payload['primary_color'] = None colors_payload['primary_color'] = None
elif isinstance(primary_color, int): elif isinstance(primary_colour, int):
colors_payload['primary_color'] = primary_color colors_payload['primary_color'] = primary_colour
else: else:
colors_payload['primary_color'] = primary_color.value colors_payload['primary_color'] = primary_colour.value
if secondary_color is not MISSING: if secondary_colour is not MISSING:
if secondary_color is None: if secondary_colour is None:
colors_payload['secondary_color'] = None colors_payload['secondary_color'] = None
elif isinstance(secondary_color, int): elif isinstance(secondary_colour, int):
colors_payload['secondary_color'] = secondary_color colors_payload['secondary_color'] = secondary_colour
else: else:
colors_payload['secondary_color'] = secondary_color.value colors_payload['secondary_color'] = secondary_colour.value
if tertiary_color is not MISSING: if tertiary_colour is not MISSING:
if tertiary_color is None: if tertiary_colour is None:
colors_payload['tertiary_color'] = None colors_payload['tertiary_color'] = None
elif isinstance(tertiary_color, int): elif isinstance(tertiary_colour, int):
colors_payload['tertiary_color'] = tertiary_color colors_payload['tertiary_color'] = tertiary_colour
else: else:
colors_payload['tertiary_color'] = tertiary_color.value colors_payload['tertiary_color'] = tertiary_colour.value
if colors_payload: if colors_payload:
payload['colors'] = colors_payload payload['colors'] = colors_payload

Loading…
Cancel
Save