Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit b504374

Browse files
authored
Add the rest of the new role icons implementation (#9)
Related #4
1 parent b11c187 commit b504374

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

discord/audit_logs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def _transform_overwrites(
118118
def _transform_icon(entry: AuditLogEntry, data: Optional[str]) -> Optional[Asset]:
119119
if data is None:
120120
return None
121+
# Check if role got changed, instead of guild icon.
122+
if entry.action.name.startswith("role"):
123+
return Asset._from_icon(entry._state, entry.id, data, "role")
121124
return Asset._from_guild_icon(entry._state, entry.guild.id, data)
122125

123126

discord/guild.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,7 @@ async def create_role(
23992399
permissions: Permissions = ...,
24002400
colour: Union[Colour, int] = ...,
24012401
icon: Optional[bytes] = ...,
2402+
emoji_unicode: Optional[str] = ...,
24022403
hoist: bool = ...,
24032404
mentionable: bool = ...,
24042405
) -> Role:
@@ -2413,6 +2414,7 @@ async def create_role(
24132414
permissions: Permissions = ...,
24142415
color: Union[Colour, int] = ...,
24152416
icon: Optional[bytes] = ...,
2417+
emoji_unicode: Optional[str] = ...,
24162418
hoist: bool = ...,
24172419
mentionable: bool = ...,
24182420
) -> Role:
@@ -2426,6 +2428,7 @@ async def create_role(
24262428
color: Union[Colour, int] = MISSING,
24272429
colour: Union[Colour, int] = MISSING,
24282430
icon: Optional[bytes] = MISSING,
2431+
emoji_unicode: Optional[str] = MISSING,
24292432
hoist: bool = MISSING,
24302433
mentionable: bool = MISSING,
24312434
reason: Optional[str] = None,
@@ -2443,7 +2446,7 @@ async def create_role(
24432446
Can now pass ``int`` to ``colour`` keyword-only parameter.
24442447
24452448
.. versionchanged:: 2.0
2446-
Added ``icon`` parameter.
2449+
Added ``icon`` and ``emoji_unicode`` parameter.
24472450
24482451
Parameters
24492452
-----------
@@ -2455,7 +2458,14 @@ async def create_role(
24552458
The colour for the role. Defaults to :meth:`Colour.default`.
24562459
This is aliased to ``color`` as well.
24572460
icon: Optional[:class:`bytes`]
2458-
The raw bytes for the role icon.
2461+
A bytes object representing the role icon.
2462+
2463+
.. versionadded:: 2.0
2464+
2465+
emoji_unicode: Optional[:class:`str`]
2466+
The unicode value of the new role to change to.
2467+
2468+
.. versionadded:: 2.0
24592469
hoist: :class:`bool`
24602470
Indicates if the role should be shown separately in the member list.
24612471
Defaults to ``False``.
@@ -2506,6 +2516,9 @@ async def create_role(
25062516
if name is not MISSING:
25072517
fields["name"] = name
25082518

2519+
if emoji_unicode is not MISSING:
2520+
fields["unicode_emoji"] = emoji_unicode
2521+
25092522
data = await self._state.http.create_role(self.id, reason=reason, **fields)
25102523
role = Role(guild=self, data=data, state=self._state)
25112524

discord/role.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .colour import Colour
3131
from .errors import InvalidArgument
3232
from .mixins import Hashable
33+
from .partial_emoji import PartialEmoji
3334
from .permissions import Permissions
3435
from .utils import MISSING, _bytes_to_base64_data, _get_as_snowflake, snowflake_time
3536

@@ -245,6 +246,7 @@ def _update(self, data: RolePayload):
245246
self.managed: bool = data.get("managed", False)
246247
self.mentionable: bool = data.get("mentionable", False)
247248
self._icon: str = data.get("icon", None)
249+
self._emoji_unicode: str = data.get("unicode_emoji", None)
248250
self.tags: Optional[RoleTags]
249251

250252
try:
@@ -324,7 +326,7 @@ def members(self) -> List[Member]:
324326
def icon(self) -> Optional[Asset]:
325327
"""Optional[:class:`Asset`]: Returns the icon asset associated with this role, can be ``None``.
326328
327-
.. versionadded: 2.0
329+
.. versionadded:: 2.0
328330
329331
.. note::
330332
@@ -334,6 +336,21 @@ def icon(self) -> Optional[Asset]:
334336
return None
335337
return Asset._from_icon(self._state, self.id, self._icon, "role")
336338

339+
@property
340+
def emoji(self) -> Optional[PartialEmoji]:
341+
"""Optional[:class:`PartialEmoji`]: Returns the emoji that associated with this role, can be ``None``.
342+
343+
.. versionadded:: 2.0
344+
345+
.. note::
346+
347+
The guild needs to have the ``ROLE_ICONS`` features to have this enabled.
348+
349+
"""
350+
if not self._emoji_unicode:
351+
return None
352+
return PartialEmoji.from_str(self._emoji_unicode)
353+
337354
async def _move(self, position: int, reason: Optional[str]) -> None:
338355
if position <= 0:
339356
raise InvalidArgument("Cannot move role to position 0 or below")
@@ -365,6 +382,7 @@ async def edit(
365382
colour: Union[Colour, int] = MISSING,
366383
color: Union[Colour, int] = MISSING,
367384
icon: Optional[bytes] = MISSING,
385+
emoji_unicode: Optional[str] = MISSING,
368386
hoist: bool = MISSING,
369387
mentionable: bool = MISSING,
370388
position: int = MISSING,
@@ -384,7 +402,7 @@ async def edit(
384402
385403
.. versionchanged:: 2.0
386404
Edits are no longer in-place, the newly edited role is returned instead.
387-
Also added a new ``icon`` parameter to change role icons.
405+
Also added a new ``icon`` and ``emoji_unicode`` parameter to change role icons.
388406
389407
Parameters
390408
-----------
@@ -396,6 +414,13 @@ async def edit(
396414
The new colour to change to. (aliased to color as well)
397415
icon: :class:`bytes`
398416
A bytes object representing the role icon.
417+
418+
.. versionadded:: 2.0
419+
420+
emoji_unicode: :class:`str`
421+
The unicode value of the new role to change to.
422+
423+
.. versionadded:: 2.0
399424
hoist: :class:`bool`
400425
Indicates if the role should be shown separately in the member list.
401426
mentionable: :class:`bool`
@@ -446,6 +471,9 @@ async def edit(
446471
else:
447472
payload["icon"] = _bytes_to_base64_data(icon)
448473

474+
if emoji_unicode is not MISSING:
475+
payload["unicode_emoji"] = emoji_unicode
476+
449477
if hoist is not MISSING:
450478
payload["hoist"] = hoist
451479

discord/types/role.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
class _RoleOptional(TypedDict, total=False):
3333
tags: RoleTags
3434
icon: str
35+
unicode_emoji: str
3536

3637

3738
class Role(_RoleOptional):

docs/api.rst

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,13 +2101,18 @@ of :class:`enum.Enum`.
21012101
When this is the action, the type of :attr:`~AuditLogEntry.target` is
21022102
the :class:`Role` or a :class:`Object` with the ID.
21032103

2104+
.. versionchanged:: 2.0
2105+
Added :attr:`~AuditLogDiff.icon` and :attr:`~AuditLogDiff.unicode_emoji`.
2106+
21042107
Possible attributes for :class:`AuditLogDiff`:
21052108

21062109
- :attr:`~AuditLogDiff.colour`
21072110
- :attr:`~AuditLogDiff.mentionable`
21082111
- :attr:`~AuditLogDiff.hoist`
21092112
- :attr:`~AuditLogDiff.name`
21102113
- :attr:`~AuditLogDiff.permissions`
2114+
- :attr:`~AuditLogDiff.icon`
2115+
- :attr:`~AuditLogDiff.unicode_emoji`
21112116

21122117
.. attribute:: role_update
21132118

@@ -2121,13 +2126,18 @@ of :class:`enum.Enum`.
21212126
When this is the action, the type of :attr:`~AuditLogEntry.target` is
21222127
the :class:`Role` or a :class:`Object` with the ID.
21232128

2129+
.. versionchanged:: 2.0
2130+
Added :attr:`~AuditLogDiff.icon` and :attr:`~AuditLogDiff.unicode_emoji`.
2131+
21242132
Possible attributes for :class:`AuditLogDiff`:
21252133

21262134
- :attr:`~AuditLogDiff.colour`
21272135
- :attr:`~AuditLogDiff.mentionable`
21282136
- :attr:`~AuditLogDiff.hoist`
21292137
- :attr:`~AuditLogDiff.name`
21302138
- :attr:`~AuditLogDiff.permissions`
2139+
- :attr:`~AuditLogDiff.icon`
2140+
- :attr:`~AuditLogDiff.unicode_emoji`
21312141

21322142
.. attribute:: role_delete
21332143

@@ -2136,13 +2146,18 @@ of :class:`enum.Enum`.
21362146
When this is the action, the type of :attr:`~AuditLogEntry.target` is
21372147
the :class:`Role` or a :class:`Object` with the ID.
21382148

2149+
.. versionchanged:: 2.0
2150+
Added :attr:`~AuditLogDiff.icon` and :attr:`~AuditLogDiff.unicode_emoji`.
2151+
21392152
Possible attributes for :class:`AuditLogDiff`:
21402153

21412154
- :attr:`~AuditLogDiff.colour`
21422155
- :attr:`~AuditLogDiff.mentionable`
21432156
- :attr:`~AuditLogDiff.hoist`
21442157
- :attr:`~AuditLogDiff.name`
21452158
- :attr:`~AuditLogDiff.permissions`
2159+
- :attr:`~AuditLogDiff.icon`
2160+
- :attr:`~AuditLogDiff.unicode_emoji`
21462161

21472162
.. attribute:: invite_create
21482163

@@ -2940,7 +2955,9 @@ AuditLogDiff
29402955

29412956
.. attribute:: icon
29422957

2943-
A guild's icon. See also :attr:`Guild.icon`.
2958+
Depending on the action type, this may be a guild's icon or a role's icon.
2959+
2960+
See also :attr:`Guild.icon` or :attr:`Role.icon`.
29442961

29452962
:type: :class:`Asset`
29462963

@@ -3360,6 +3377,12 @@ AuditLogDiff
33603377

33613378
:type: :class:`int`
33623379

3380+
.. attribute:: unicode_emoji
3381+
3382+
The unicode emoji that represents when role got changed.
3383+
3384+
:type: :class:`str`
3385+
33633386
.. this is currently missing the following keys: reason and application_id
33643387
I'm not sure how to about porting these
33653388

0 commit comments

Comments
 (0)