Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class Guild(Hashable):
- ``PARTNERED``: Guild is a partnered server.
- ``PREVIEW_ENABLED``: Guild can be viewed before being accepted via Membership Screening.
- ``PRIVATE_THREADS``: Guild has access to create private threads.
- ``ROLE_ICONS``: Guild has access to set icon for roles.
- ``SEVEN_DAY_THREAD_ARCHIVE``: Guild has access to the seven day archive time for threads.
- ``THREE_DAY_THREAD_ARCHIVE``: Guild has access to the three day archive time for threads.
- ``TICKETED_EVENTS_ENABLED``: Guild has enabled ticketed events.
Expand Down Expand Up @@ -2397,6 +2398,7 @@ async def create_role(
name: str = ...,
permissions: Permissions = ...,
colour: Union[Colour, int] = ...,
icon: Optional[bytes] = ...,
hoist: bool = ...,
mentionable: bool = ...,
) -> Role:
Expand All @@ -2410,6 +2412,7 @@ async def create_role(
name: str = ...,
permissions: Permissions = ...,
color: Union[Colour, int] = ...,
icon: Optional[bytes] = ...,
hoist: bool = ...,
mentionable: bool = ...,
) -> Role:
Expand All @@ -2422,6 +2425,7 @@ async def create_role(
permissions: Permissions = MISSING,
color: Union[Colour, int] = MISSING,
colour: Union[Colour, int] = MISSING,
icon: Optional[bytes] = MISSING,
hoist: bool = MISSING,
mentionable: bool = MISSING,
reason: Optional[str] = None,
Expand All @@ -2438,6 +2442,9 @@ async def create_role(
.. versionchanged:: 1.6
Can now pass ``int`` to ``colour`` keyword-only parameter.

.. versionchanged:: 2.0
Added ``icon`` parameter.

Parameters
-----------
name: :class:`str`
Expand All @@ -2447,6 +2454,8 @@ 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.
icon: Optional[:class:`bytes`]
The raw bytes for the role icon.
hoist: :class:`bool`
Indicates if the role should be shown separately in the member list.
Defaults to ``False``.
Expand Down Expand Up @@ -2482,6 +2491,12 @@ async def create_role(
else:
fields["color"] = actual_colour.value

if icon is not MISSING:
if icon is None:
fields["icon"] = icon
else:
fields["icon"] = utils._bytes_to_base64_data(icon)

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

Expand Down
2 changes: 1 addition & 1 deletion discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Attachment(Hashable):
.. versionadded:: 2.0
"""

__slots__ = ("id", "size", "height", "width", "filename", "url", "proxy_url", "_http", "content_type")
__slots__ = ("id", "size", "height", "width", "filename", "url", "proxy_url", "_http", "content_type", "ephemeral")

def __init__(self, *, data: AttachmentPayload, state: ConnectionState):
self.id: int = int(data["id"])
Expand Down
29 changes: 28 additions & 1 deletion discord/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeVar, Union

from .asset import Asset
from .colour import Colour
from .errors import InvalidArgument
from .mixins import Hashable
from .permissions import Permissions
from .utils import MISSING, _get_as_snowflake, snowflake_time
from .utils import MISSING, _bytes_to_base64_data, _get_as_snowflake, snowflake_time

__all__ = (
"RoleTags",
Expand Down Expand Up @@ -176,6 +177,7 @@ class Role(Hashable):
"id",
"name",
"_permissions",
"_icon",
"_colour",
"position",
"managed",
Expand Down Expand Up @@ -242,6 +244,7 @@ def _update(self, data: RolePayload):
self.hoist: bool = data.get("hoist", False)
self.managed: bool = data.get("managed", False)
self.mentionable: bool = data.get("mentionable", False)
self._icon: str = data.get("icon", None)
self.tags: Optional[RoleTags]

try:
Expand Down Expand Up @@ -317,6 +320,20 @@ def members(self) -> List[Member]:
role_id = self.id
return [member for member in all_members if member._roles.has(role_id)]

@property
def icon(self) -> Optional[Asset]:
"""Optional[:class:`Asset`]: Returns the icon asset associated with this role, can be ``None``.

.. versionadded: 2.0

.. note::

The guild needs to have the ``ROLE_ICONS`` features to have this enabled.
"""
if not self._icon:
return None
return Asset._from_icon(self._state, self.id, self._icon, "role")

async def _move(self, position: int, reason: Optional[str]) -> None:
if position <= 0:
raise InvalidArgument("Cannot move role to position 0 or below")
Expand Down Expand Up @@ -347,6 +364,7 @@ async def edit(
permissions: Permissions = MISSING,
colour: Union[Colour, int] = MISSING,
color: Union[Colour, int] = MISSING,
icon: Optional[bytes] = MISSING,
hoist: bool = MISSING,
mentionable: bool = MISSING,
position: int = MISSING,
Expand All @@ -366,6 +384,7 @@ async def edit(

.. versionchanged:: 2.0
Edits are no longer in-place, the newly edited role is returned instead.
Also added a new ``icon`` parameter to change role icons.

Parameters
-----------
Expand All @@ -375,6 +394,8 @@ 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)
icon: :class:`bytes`
A bytes object representing the role icon.
hoist: :class:`bool`
Indicates if the role should be shown separately in the member list.
mentionable: :class:`bool`
Expand Down Expand Up @@ -419,6 +440,12 @@ async def edit(
if permissions is not MISSING:
payload["permissions"] = permissions.value

if icon is not MISSING:
if icon is None:
payload["icon"] = icon
else:
payload["icon"] = _bytes_to_base64_data(icon)

if hoist is not MISSING:
payload["hoist"] = hoist

Expand Down
1 change: 1 addition & 0 deletions discord/types/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class _GuildOptional(TypedDict, total=False):
"PARTNERED",
"PREVIEW_ENABLED",
"PRIVATE_THREADS",
"ROLE_ICONS",
"SEVEN_DAY_THREAD_ARCHIVE",
"THREE_DAY_THREAD_ARCHIVE",
"TICKETED_EVENTS_ENABLED",
Expand Down
1 change: 1 addition & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class _AttachmentOptional(TypedDict, total=False):
width: Optional[int]
content_type: str
spoiler: bool
ephemeral: bool


class Attachment(_AttachmentOptional):
Expand Down
1 change: 1 addition & 0 deletions discord/types/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

class _RoleOptional(TypedDict, total=False):
tags: RoleTags
icon: str


class Role(_RoleOptional):
Expand Down