Browse Source

Merge e5991a84b9 into 1add8a0b40

pull/10428/merge
Soheab 2 weeks ago
committed by GitHub
parent
commit
18c11d6cfc
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      discord/__init__.py
  2. 11
      discord/abc.py
  3. 9
      discord/enums.py
  4. 16
      discord/ext/commands/context.py
  5. 5
      discord/http.py
  6. 22
      discord/message.py
  7. 176
      discord/shared_client_theme.py
  8. 11
      discord/types/message.py
  9. 37
      docs/api.rst

1
discord/__init__.py

@ -76,6 +76,7 @@ from .presences import *
from .primary_guild import *
from .onboarding import *
from .collectible import *
from .shared_client_theme import *
class VersionInfo(NamedTuple):

11
discord/abc.py

@ -113,6 +113,7 @@ if TYPE_CHECKING:
SnowflakeList,
)
from .permissions import _PermissionOverwriteKwargs
from .shared_client_theme import SharedClientTheme
PartialMessageableChannel = Union[TextChannel, VoiceChannel, StageChannel, Thread, DMChannel, PartialMessageable]
MessageableChannel = Union[PartialMessageableChannel, GroupChannel]
@ -1458,6 +1459,7 @@ class Messageable:
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -1478,6 +1480,7 @@ class Messageable:
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -1498,6 +1501,7 @@ class Messageable:
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -1518,6 +1522,7 @@ class Messageable:
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
async def send(
@ -1539,6 +1544,7 @@ class Messageable:
suppress_embeds: bool = False,
silent: bool = False,
poll: Optional[Poll] = None,
shared_client_theme: Optional[SharedClientTheme] = None,
) -> Message:
"""|coro|
@ -1629,6 +1635,10 @@ class Messageable:
The poll to send with this message.
.. versionadded:: 2.4
shared_client_theme: Optional[:class:`~discord.SharedClientTheme`]
The shared client theme to send with this message.
.. versionadded:: 2.8
Raises
--------
@ -1702,6 +1712,7 @@ class Messageable:
view=view,
flags=flags,
poll=poll,
shared_client_theme=shared_client_theme,
) as params:
data = await state.http.send_message(channel.id, params=params)

9
discord/enums.py

@ -87,6 +87,7 @@ __all__ = (
'MediaItemLoadingState',
'CollectibleType',
'NameplatePalette',
'BaseTheme',
)
@ -1006,6 +1007,14 @@ class NameplatePalette(Enum):
white = 'white'
class BaseTheme(Enum):
unset = 0
light = 1
dark = 2
darker = 3
midnight = 4
def create_unknown_value(cls: Type[E], val: Any) -> E:
value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below
name = f'unknown_{val}'

16
discord/ext/commands/context.py

@ -52,6 +52,7 @@ if TYPE_CHECKING:
from discord.ui.view import BaseView, View, LayoutView
from discord.types.interactions import ApplicationCommandInteractionData
from discord.poll import Poll
from discord.shared_client_theme import SharedClientTheme
from .cog import Cog
from .core import Command
@ -681,6 +682,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -702,6 +704,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -723,6 +726,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -744,6 +748,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
async def reply(self, content: Optional[str] = None, **kwargs: Any) -> Message:
@ -898,6 +903,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -919,6 +925,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -940,6 +947,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -961,6 +969,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
async def send(
@ -983,6 +992,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
ephemeral: bool = False,
silent: bool = False,
poll: Optional[Poll] = None,
shared_client_theme: Optional[SharedClientTheme] = None,
) -> Message:
"""|coro|
@ -1078,6 +1088,10 @@ class Context(discord.abc.Messageable, Generic[BotT]):
.. versionadded:: 2.4
.. versionchanged:: 2.6
This can now be ``None`` and defaults to ``None`` instead of ``MISSING``.
shared_client_theme: Optional[:class:`~discord.SharedClientTheme`]
The shared client theme to send with this message.
.. versionadded:: 2.8
Raises
--------
@ -1117,6 +1131,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
suppress_embeds=suppress_embeds,
silent=silent,
poll=poll,
shared_client_theme=shared_client_theme,
) # type: ignore # The overloads don't support Optional but the implementation does
# Convert the kwargs from None to MISSING to appease the remaining implementations
@ -1133,6 +1148,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
'ephemeral': ephemeral,
'silent': silent,
'poll': MISSING if poll is None else poll,
'shared_client_theme': MISSING if shared_client_theme is None else shared_client_theme,
}
if self.interaction.response.is_done():

5
discord/http.py

@ -68,6 +68,7 @@ if TYPE_CHECKING:
from .embeds import Embed
from .message import Attachment
from .poll import Poll
from .shared_client_theme import SharedClientTheme
from .types import (
appinfo,
@ -161,6 +162,7 @@ def handle_message_parameters(
channel_payload: Dict[str, Any] = MISSING,
applied_tags: Optional[SnowflakeList] = MISSING,
poll: Optional[Poll] = MISSING,
shared_client_theme: Optional[SharedClientTheme] = MISSING,
) -> MultipartParameters:
if files is not MISSING and file is not MISSING:
raise TypeError('Cannot mix file and files keyword arguments.')
@ -206,6 +208,9 @@ def handle_message_parameters(
else:
payload['components'] = []
if shared_client_theme not in (MISSING, None):
payload['shared_client_theme'] = shared_client_theme.to_dict()
if nonce is not None:
payload['nonce'] = str(nonce)
payload['enforce_nonce'] = True

22
discord/message.py

@ -65,6 +65,7 @@ from .sticker import StickerItem, GuildSticker
from .threads import Thread
from .channel import PartialMessageable
from .poll import Poll
from .shared_client_theme import SharedClientTheme
if TYPE_CHECKING:
from typing_extensions import Self
@ -81,6 +82,7 @@ if TYPE_CHECKING:
CallMessage as CallMessagePayload,
PurchaseNotificationResponse as PurchaseNotificationResponsePayload,
GuildProductPurchase as GuildProductPurchasePayload,
SharedClientTheme as SharedClientThemePayload,
)
from .types.interactions import MessageInteraction as MessageInteractionPayload
@ -1781,6 +1783,7 @@ class PartialMessage(Hashable):
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -1801,6 +1804,7 @@ class PartialMessage(Hashable):
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -1821,6 +1825,7 @@ class PartialMessage(Hashable):
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
@overload
@ -1841,6 +1846,7 @@ class PartialMessage(Hashable):
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
shared_client_theme: SharedClientTheme = ...,
) -> Message: ...
async def reply(self, content: Optional[str] = None, **kwargs: Any) -> Message:
@ -2138,6 +2144,10 @@ class Message(PartialMessage, Hashable):
The message snapshots attached to this message.
.. versionadded:: 2.5
shared_client_theme: Optional[:class:`SharedClientTheme`]
The client theme shared in this message.
.. versionadded:: 2.8
"""
__slots__ = (
@ -2178,6 +2188,7 @@ class Message(PartialMessage, Hashable):
'purchase_notification',
'message_snapshots',
'_pinned_at',
'shared_client_theme',
)
if TYPE_CHECKING:
@ -2323,6 +2334,14 @@ class Message(PartialMessage, Hashable):
else:
self.purchase_notification = PurchaseNotification(purchase_notification)
self.shared_client_theme: Optional[SharedClientTheme] = None
try:
shared_client_theme = data['shared_client_theme'] # pyright: ignore[reportTypedDictNotRequiredAccess]
except KeyError:
pass
else:
self.shared_client_theme = SharedClientTheme.from_dict(shared_client_theme)
for handler in ('author', 'member', 'mentions', 'mention_roles', 'components', 'call'):
try:
getattr(self, f'_handle_{handler}')(data[handler]) # type: ignore
@ -2509,6 +2528,9 @@ class Message(PartialMessage, Hashable):
def _handle_interaction_metadata(self, data: MessageInteractionMetadataPayload):
self.interaction_metadata = MessageInteractionMetadata(state=self._state, guild=self.guild, data=data)
def _handle_shared_client_theme(self, data: SharedClientThemePayload):
self.shared_client_theme = SharedClientTheme.from_dict(data)
def _handle_call(self, data: CallMessagePayload):
if data is not None:
self.call = CallMessage(state=self._state, message=self, data=data)

176
discord/shared_client_theme.py

@ -0,0 +1,176 @@
"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from typing import List, Sequence, Union, TYPE_CHECKING
from .enums import BaseTheme, try_enum
from .colour import Colour
from .utils import MISSING
if TYPE_CHECKING:
from typing_extensions import Self
from .types.message import SharedClientTheme as SharedClientThemePayload
__all__ = ('SharedClientTheme',)
class SharedClientTheme:
"""Represents a shared client theme from a :class:`~discord.Message`.
This can be constructed by users to create a new shared client theme for sending and
is received using :attr:`Message.shared_client_theme` when a message contains a shared client theme.
.. versionadded:: 2.8
Parameters
-----------
colours: Sequence[Union[:class:`Colour`, :class:`int`]]
A list of the theme's colours. Must be between 1 and 5 colours.
colors: Sequence[Union[:class:`Colour`, :class:`int`]]
An alias for ``colours``.
gradient_angle: :class:`int`
The direction of the theme's gradient in degrees. Must be between 0 and 360.
This is only applicable if there are at least 2 colours.
intensity: :class:`int`
The intensity of the theme's colors. Must be between 0 and 100.
theme: :class:`BaseTheme`
The base theme to use for this client theme. Defaults to :attr:`BaseTheme.dark`.
Raises
-------
ValueError
- If neither ``colours`` nor ``colors`` is provided.
- If ``colours`` is empty.
- If ``colours`` contains more than 5 colours.
- If ``gradient_angle`` is set but there are less than 2 colours.
- If ``gradient_angle`` is not between 0 and 360.
- If ``intensity`` is not between 0 and 100.
TypeError
- If ``theme`` is not an instance of :class:`BaseTheme`.
"""
__slots__ = ('_colours', '_gradient_angle', '_intensity', '_theme')
def __init__(
self,
*,
colours: Sequence[Union[Colour, int]] = MISSING,
colors: Sequence[Union[Colour, int]] = MISSING,
gradient_angle: int = 0,
intensity: int = 0,
theme: BaseTheme = BaseTheme.dark,
) -> None:
colours = colours if colours is not MISSING else colors
if colours is MISSING:
raise ValueError('missing required argument `colo(u)rs`')
self.colours = colours
self.gradient_angle = gradient_angle
self.intensity = intensity
self.theme = theme
@classmethod
def from_dict(cls, data: SharedClientThemePayload) -> Self:
return cls(
colours=[Colour(int(colour.strip('#'), 16)) for colour in data.get('colors', [])],
gradient_angle=data.get('gradient_angle', 0),
intensity=data.get('base_mix', 0),
theme=try_enum(BaseTheme, data.get('base_theme', BaseTheme.dark.value)),
)
def to_dict(self) -> SharedClientThemePayload:
return {
'colors': [f'{colour.value:06x}' for colour in self._colours],
'gradient_angle': self.gradient_angle,
'base_mix': self.intensity,
'base_theme': self.theme.value,
}
def __repr__(self) -> str:
return f'<SharedClientTheme colours={self.colours!r} gradient_angle={self.gradient_angle} intensity={self.intensity} theme={self.theme!r}>'
@property
def colours(self) -> List[Colour]:
"""List[:class:`Colour`]: A list of the theme's colours."""
return self._colours
@colours.setter
def colours(self, value: Sequence[Union[Colour, int]]) -> None:
if not value:
raise ValueError('colours must contain at least 1 colour')
if len(value) > 5:
raise ValueError('cannot have more than 5 colours')
if len(value) < 2:
self._gradient_angle = 0
self._colours = [colour if isinstance(colour, Colour) else Colour(colour) for colour in value]
colors = colours
@property
def gradient_angle(self) -> int:
""":class:`int`: The direction of the theme's gradient in degrees.
This is only applicable if there are at least 2 colours.
"""
return self._gradient_angle
@gradient_angle.setter
def gradient_angle(self, value: int) -> None:
if len(self._colours) < 2 and value != 0:
raise ValueError('gradient_angle may only be set if there are at least 2 colours')
if not 0 <= value <= 360:
raise ValueError('gradient_angle must be between 0 and 360')
self._gradient_angle = value
@property
def intensity(self) -> int:
""":class:`int`: The intensity of the theme's colors."""
return self._intensity
@intensity.setter
def intensity(self, value: int) -> None:
if not 0 <= value <= 100:
raise ValueError('intensity must be between 0 and 100')
self._intensity = value
@property
def theme(self) -> BaseTheme:
""":class:`BaseTheme`: The base theme to use for this client theme."""
return self._theme
@theme.setter
def theme(self, value: BaseTheme) -> None:
if not isinstance(value, BaseTheme):
raise TypeError('theme must be an instance of BaseTheme')
self._theme = value

11
discord/types/message.py

@ -228,6 +228,7 @@ class Message(PartialMessage):
thread: NotRequired[Thread]
call: NotRequired[CallMessage]
purchase_notification: NotRequired[PurchaseNotificationResponse]
shared_client_theme: NotRequired[SharedClientTheme]
AllowedMentionType = Literal['roles', 'users', 'everyone']
@ -248,3 +249,13 @@ class MessagePin(TypedDict):
class ChannelPins(TypedDict):
items: List[MessagePin]
has_more: bool
BaseTheme = Literal[0, 1, 2, 3, 4]
class SharedClientTheme(TypedDict):
colors: List[str]
gradient_angle: int
base_mix: int
base_theme: BaseTheme

37
docs/api.rst

@ -4154,6 +4154,35 @@ of :class:`enum.Enum`.
The collectible nameplate palette is white.
.. class:: BaseTheme
Represents the available themes for a shared client theme.
.. versionadded:: 2.8
.. attribute:: unset
The theme is unset.
This is equivalent to :attr:`dark`.
.. attribute:: dark
The shared theme is based on the dark theme.
.. attribute:: light
The shared theme is based on the light theme.
.. attribute:: darker
The shared theme is based on the darker theme.
.. attribute:: midnight
The shared theme is based on the midnight theme.
.. _discord-api-audit-logs:
Audit Log Data
@ -6181,6 +6210,14 @@ PollMedia
:members:
SharedClientTheme
~~~~~~~~~~~~~~~~~~
.. attributetable:: SharedClientTheme
.. autoclass:: SharedClientTheme
:members:
Exceptions
------------

Loading…
Cancel
Save