Browse Source

refactor: Colour assigns only primary_color and added version strings

pull/10214/head
makerze 2 weeks ago
parent
commit
98e3668214
  1. 17
      discord/guild.py
  2. 41
      discord/role.py

17
discord/guild.py

@ -3703,6 +3703,13 @@ class Guild(Hashable):
This function will now raise :exc:`TypeError` instead of This function will now raise :exc:`TypeError` instead of
``InvalidArgument``. ``InvalidArgument``.
.. versionadded:: 2.6
The ``secondary_color``, ``tertiary_color``, ``secondary_colour``, and ``tertiary_colour`` keyword-only parameters were added.
.. versionchanged:: 2.6
The ``colour`` and ``color`` parameters now set the role's primary color.
Parameters Parameters
----------- -----------
name: :class:`str` name: :class:`str`
@ -3751,19 +3758,17 @@ class Guild(Hashable):
else: else:
fields['permissions'] = '0' fields['permissions'] = '0'
colours: Dict[str, Any] = {}
actual_colour = colour or color or Colour.default() actual_colour = colour or color or Colour.default()
if isinstance(actual_colour, int): if isinstance(actual_colour, int):
fields['color'] = actual_colour colours['primary_color'] = actual_colour
else: else:
fields['color'] = actual_colour.value colours['primary_color'] = actual_colour.value
actual_secondary_colour = secondary_colour or secondary_color actual_secondary_colour = secondary_colour or secondary_color
actual_tertiary_colour = tertiary_colour or tertiary_color actual_tertiary_colour = tertiary_colour or tertiary_color
colours: Dict[str, Any] = {
'primary_color': fields['color'],
}
if actual_secondary_colour is not MISSING: if actual_secondary_colour is not MISSING:
if actual_secondary_colour is None: if actual_secondary_colour is None:
colours['secondary_color'] = None colours['secondary_color'] = None

41
discord/role.py

@ -275,10 +275,11 @@ class Role(Hashable):
return not r return not r
def _update(self, data: RolePayload): def _update(self, data: RolePayload):
colors = data.get('colors', {})
self.name: str = data['name'] self.name: str = data['name']
self._permissions: int = int(data.get('permissions', 0)) self._permissions: int = int(data.get('permissions', 0))
self.position: int = data.get('position', 0) self.position: int = data.get('position', 0)
self._colour: int = data.get('color', 0) self._colour: int = colors.get('primary_color', 0)
self.hoist: bool = data.get('hoist', False) self.hoist: bool = data.get('hoist', False)
self._icon: Optional[str] = data.get('icon') self._icon: Optional[str] = data.get('icon')
self.unicode_emoji: Optional[str] = data.get('unicode_emoji') self.unicode_emoji: Optional[str] = data.get('unicode_emoji')
@ -286,7 +287,6 @@ class Role(Hashable):
self.mentionable: bool = data.get('mentionable', False) self.mentionable: bool = data.get('mentionable', False)
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', {})
self._secondary_colour = colors.get('secondary_color', None) self._secondary_colour = colors.get('secondary_color', None)
self._tertiary_colour = colors.get('tertiary_color', None) self._tertiary_colour = colors.get('tertiary_color', None)
@ -330,22 +330,30 @@ class Role(Hashable):
@property @property
def secondary_colour(self) -> Optional[Colour]: def secondary_colour(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's secondary colour.""" """Optional[:class:`Colour`]: The role's secondary colour.
.. versionadded:: 2.6
"""
return Colour(self._secondary_colour) if self._secondary_colour is not None else None 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`]: Alias for :attr:`secondary_colour`.""" """Optional[:class:`Colour`]: Alias for :attr:`secondary_colour`.
.. versionadded:: 2.6
"""
return self.secondary_colour return self.secondary_colour
@property @property
def tertiary_colour(self) -> Optional[Colour]: def tertiary_colour(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: The role's tertiary colour.""" """Optional[:class:`Colour`]: The role's tertiary colour.
.. versionadded:: 2.6
"""
return Colour(self._tertiary_colour) if self._tertiary_colour is not None else None 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`]: Alias for :attr:`tertiary_colour`.""" """Optional[:class:`Colour`]: Alias for :attr:`tertiary_colour`.
.. versionadded:: 2.6
"""
return self.tertiary_colour return self.tertiary_colour
@property @property
@ -355,12 +363,12 @@ class Role(Hashable):
@property @property
def colour(self) -> Colour: def colour(self) -> Colour:
""":class:`Colour`: Returns the role colour. An alias exists under ``color``.""" """:class:`Colour`: Returns the role's primary colour. An alias exists under ``color``."""
return Colour(self._colour) return Colour(self._colour)
@property @property
def color(self) -> Colour: def color(self) -> Colour:
""":class:`Colour`: Returns the role color. An alias exists under ``colour``.""" """:class:`Colour`: Returns the role's primary colour. An alias exists under ``colour``."""
return self.colour return self.colour
@property @property
@ -476,6 +484,12 @@ class Role(Hashable):
This function will now raise :exc:`ValueError` instead of This function will now raise :exc:`ValueError` instead of
``InvalidArgument``. ``InvalidArgument``.
.. versionadded:: 2.6
The ``secondary_color``, ``tertiary_color``, ``secondary_colour``, and ``tertiary_colour`` keyword-only parameters were added.
.. versionchanged:: 2.6
The ``colour`` and ``color`` parameters now set the role's primary color.
Parameters Parameters
----------- -----------
name: :class:`str` name: :class:`str`
@ -524,14 +538,17 @@ class Role(Hashable):
await self._move(position, reason=reason) await self._move(position, reason=reason)
payload: Dict[str, Any] = {} payload: Dict[str, Any] = {}
colours: Dict[str, Any] = {}
if color is not MISSING: if color is not MISSING:
colour = color colour = color
if colour is not MISSING: if colour is not MISSING:
if isinstance(colour, int): if isinstance(colour, int):
payload['color'] = colour colours['primary_color'] = colour
else: else:
payload['color'] = colour.value colours['primary_color'] = colour.value
if name is not MISSING: if name is not MISSING:
payload['name'] = name payload['name'] = name
@ -553,10 +570,6 @@ class Role(Hashable):
if mentionable is not MISSING: if mentionable is not MISSING:
payload['mentionable'] = mentionable payload['mentionable'] = mentionable
colours: Dict[str, Any] = {
'primary_color': payload['color'],
}
actual_secondary_colour = secondary_colour or secondary_color actual_secondary_colour = secondary_colour or secondary_color
actual_tertiary_colour = tertiary_colour or tertiary_color actual_tertiary_colour = tertiary_colour or tertiary_color

Loading…
Cancel
Save