Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,9 @@ async def create_role(
hoist: bool = ...,
display_icon: Union[bytes, str] = MISSING,
mentionable: bool = ...,
primary_colour: Union[Colour, int, None] = ...,
secondary_colour: Union[Colour, int, None] = ...,
tertiary_colour: Union[Colour, int, None] = ...,
) -> Role:
...

Expand All @@ -3662,6 +3665,9 @@ async def create_role(
hoist: bool = ...,
display_icon: Union[bytes, str] = MISSING,
mentionable: bool = ...,
primary_color: Union[Colour, int, None] = ...,
secondary_color: Union[Colour, int, None] = ...,
tertiary_color: Union[Colour, int, None] = ...,
) -> Role:
...

Expand All @@ -3676,6 +3682,12 @@ async def create_role(
display_icon: Union[bytes, str] = MISSING,
mentionable: bool = MISSING,
reason: Optional[str] = None,
primary_color: Union[Colour, int, None] = MISSING,
secondary_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:
"""|coro|

Expand Down Expand Up @@ -3704,6 +3716,13 @@ async def create_role(
colour: Union[:class:`Colour`, :class:`int`]
The colour for the role. Defaults to :meth:`Colour.default`.
This is aliased to ``color`` as well.
primary_colour: Union[:class:`Colour`, :class:`int`, None]
The primary colour for the role. If provided, must be an integer or :class:`Colour`.
secondary_colour: Union[:class:`Colour`, :class:`int`, None]
The secondary colour for the role.
tertiary_colour: Union[:class:`Colour`, :class:`int`, None]
The tertiary colour for the role. Can only be used for the holographic role preset,
which is ``(11127295, 16759788, 16761760)``
hoist: :class:`bool`
Indicates if the role should be shown separately in the member list.
Defaults to ``False``.
Expand Down Expand Up @@ -3744,6 +3763,56 @@ async def create_role(
else:
fields['color'] = actual_colour.value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the lines above should just be on colors.primary_color, not on color anymore.


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
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:
raise TypeError(
"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] = {}
if actual_primary_colour is not MISSING:
if actual_primary_colour is None:
colors_payload['primary_color'] = None
elif isinstance(actual_primary_colour, int):
colors_payload['primary_color'] = actual_primary_colour
else:
colors_payload['primary_color'] = actual_primary_colour.value
if actual_secondary_colour is not MISSING:
if actual_secondary_colour is None:
colors_payload['secondary_color'] = None
elif isinstance(actual_secondary_colour, int):
colors_payload['secondary_color'] = actual_secondary_colour
else:
colors_payload['secondary_color'] = actual_secondary_colour.value
if actual_tertiary_colour is not MISSING:
if actual_tertiary_colour is None:
colors_payload['tertiary_color'] = None
elif isinstance(actual_tertiary_colour, int):
colors_payload['tertiary_color'] = actual_tertiary_colour
else:
colors_payload['tertiary_color'] = actual_tertiary_colour.value

if colors_payload:
fields['colors'] = colors_payload

if not colors_payload:
actual_colour = colour or color or Colour.default()
if isinstance(actual_colour, int):
fields['color'] = actual_colour
else:
fields['color'] = actual_colour.value

if hoist is not MISSING:
fields['hoist'] = hoist

Expand Down
2 changes: 1 addition & 1 deletion discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,7 @@ def edit_role(
self, guild_id: Snowflake, role_id: Snowflake, *, reason: Optional[str] = None, **fields: Any
) -> Response[role.Role]:
r = Route('PATCH', '/guilds/{guild_id}/roles/{role_id}', guild_id=guild_id, role_id=role_id)
valid_keys = ('name', 'permissions', 'color', 'hoist', 'icon', 'unicode_emoji', 'mentionable')
valid_keys = ('name', 'permissions', 'color', 'hoist', 'icon', 'unicode_emoji', 'mentionable', 'colors')
payload = {k: v for k, v in fields.items() if k in valid_keys}
return self.request(r, json=payload, reason=reason)

Expand Down
98 changes: 98 additions & 0 deletions discord/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ class Role(Hashable):
'tags',
'_flags',
'_state',
'_primary_colour',
'_secondary_colour',
'_tertiary_colour',
)

def __init__(self, *, guild: Guild, state: ConnectionState, data: RolePayload):
Expand Down Expand Up @@ -284,6 +287,10 @@ def _update(self, data: RolePayload):
self.mentionable: bool = data.get('mentionable', False)
self.tags: Optional[RoleTags]
self._flags: int = data.get('flags', 0)
colors = data.get('colors', {})
self._primary_colour = colors.get('primary_colour', None)
self._secondary_colour = colors.get('secondary_colour', None)
self._tertiary_colour = colors.get('tertiary_colour', None)

try:
self.tags = RoleTags(data['tags']) # pyright: ignore[reportTypedDictNotRequiredAccess]
Expand Down Expand Up @@ -323,6 +330,36 @@ def is_assignable(self) -> bool:
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)

@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
def primary_color(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: Alias for :attr:`primary_colour`."""
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
def secondary_color(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: Alias for :attr:`secondary_colour`."""
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
def tertiary_color(self) -> Optional[Colour]:
"""Optional[:class:`Colour`]: Alias for :attr:`tertiary_colour`."""
return self.tertiary_colour

@property
def permissions(self) -> Permissions:
""":class:`Permissions`: Returns the role's permissions."""
Expand Down Expand Up @@ -425,6 +462,12 @@ async def edit(
mentionable: bool = MISSING,
position: int = MISSING,
reason: Optional[str] = MISSING,
primary_color: Union[Colour, int, None] = MISSING,
secondary_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]:
"""|coro|

Expand Down Expand Up @@ -455,6 +498,13 @@ async def edit(
The new permissions to change to.
colour: Union[:class:`Colour`, :class:`int`]
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`
Indicates if the role should be shown separately in the member list.
display_icon: Optional[Union[:class:`bytes`, :class:`str`]]
Expand Down Expand Up @@ -519,6 +569,54 @@ async def edit(
if mentionable is not MISSING:
payload['mentionable'] = mentionable

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
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:
raise TypeError(
"You must choose either only solid colour (color/colour) or colours (primary_colour/secondary_colour/tertiary_colour), not both."
)

if primary_color is not MISSING:
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
elif isinstance(primary_colour, int):
colors_payload['primary_color'] = primary_colour
else:
colors_payload['primary_color'] = primary_colour.value
if secondary_colour is not MISSING:
if secondary_colour is None:
colors_payload['secondary_color'] = None
elif isinstance(secondary_colour, int):
colors_payload['secondary_color'] = secondary_colour
else:
colors_payload['secondary_color'] = secondary_colour.value
if tertiary_colour is not MISSING:
if tertiary_colour is None:
colors_payload['tertiary_color'] = None
elif isinstance(tertiary_colour, int):
colors_payload['tertiary_color'] = tertiary_colour
else:
colors_payload['tertiary_color'] = tertiary_colour.value
if colors_payload:
payload['colors'] = colors_payload

data = await self._state.http.edit_role(self.guild.id, self.id, reason=reason, **payload)
return Role(guild=self.guild, data=data, state=self._state)

Expand Down
Loading