From 406f0ffe04066a444bea326d5c2010fe6acdf0ec Mon Sep 17 00:00:00 2001 From: Arthur Jovart Date: Sat, 28 Aug 2021 23:14:26 +0200 Subject: [PATCH 1/2] Make `Embed.image` and `Embed.thumbnail` full-featured properties This avoids the need for set_* methods. --- discord/embeds.py | 80 +++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/discord/embeds.py b/discord/embeds.py index 7033a10e..e2119258 100644 --- a/discord/embeds.py +++ b/discord/embeds.py @@ -72,30 +72,36 @@ if TYPE_CHECKING: T = TypeVar('T') MaybeEmpty = Union[T, _EmptyEmbed] + class _EmbedFooterProxy(Protocol): text: MaybeEmpty[str] icon_url: MaybeEmpty[str] + class _EmbedFieldProxy(Protocol): name: MaybeEmpty[str] value: MaybeEmpty[str] inline: bool + class _EmbedMediaProxy(Protocol): url: MaybeEmpty[str] proxy_url: MaybeEmpty[str] height: MaybeEmpty[int] width: MaybeEmpty[int] + class _EmbedVideoProxy(Protocol): url: MaybeEmpty[str] height: MaybeEmpty[int] width: MaybeEmpty[int] + class _EmbedProviderProxy(Protocol): name: MaybeEmpty[str] url: MaybeEmpty[str] + class _EmbedAuthorProxy(Protocol): name: MaybeEmpty[str] url: MaybeEmpty[str] @@ -175,15 +181,15 @@ class Embed: Empty: Final = EmptyEmbed def __init__( - self, - *, - colour: Union[int, Colour, _EmptyEmbed] = EmptyEmbed, - color: Union[int, Colour, _EmptyEmbed] = EmptyEmbed, - title: MaybeEmpty[Any] = EmptyEmbed, - type: EmbedType = 'rich', - url: MaybeEmpty[Any] = EmptyEmbed, - description: MaybeEmpty[Any] = EmptyEmbed, - timestamp: datetime.datetime = None, + self, + *, + colour: Union[int, Colour, _EmptyEmbed] = EmptyEmbed, + color: Union[int, Colour, _EmptyEmbed] = EmptyEmbed, + title: MaybeEmpty[Any] = EmptyEmbed, + type: EmbedType = 'rich', + url: MaybeEmpty[Any] = EmptyEmbed, + description: MaybeEmpty[Any] = EmptyEmbed, + timestamp: datetime.datetime = None, ): self.colour = colour if colour is not EmptyEmbed else color @@ -366,7 +372,7 @@ class Embed: self._footer['icon_url'] = str(icon_url) return self - + def remove_footer(self: E) -> E: """Clears embed's footer information. @@ -381,7 +387,7 @@ class Embed: pass return self - + @property def image(self) -> _EmbedMediaProxy: """Returns an ``EmbedProxy`` denoting the image contents. @@ -397,6 +403,19 @@ class Embed: """ return EmbedProxy(getattr(self, '_image', {})) # type: ignore + @image.setter + def image(self: E, *, url: Any): + self._image = { + 'url': str(url), + } + + @image.deleter + def image(self: E): + try: + del self._image + except AttributeError: + pass + def set_image(self: E, *, url: MaybeEmpty[Any]) -> E: """Sets the image for the embed content. @@ -413,14 +432,9 @@ class Embed: """ if url is EmptyEmbed: - try: - del self._image - except AttributeError: - pass + del self.image else: - self._image = { - 'url': str(url), - } + self.image = url return self @@ -439,7 +453,25 @@ class Embed: """ return EmbedProxy(getattr(self, '_thumbnail', {})) # type: ignore - def set_thumbnail(self: E, *, url: MaybeEmpty[Any]) -> E: + @thumbnail.setter + def thumbnail(self: E, *, url: Any): + """Sets the thumbnail for the embed content. + """ + + self._thumbnail = { + 'url': str(url), + } + + return + + @thumbnail.deleter + def thumbnail(self): + try: + del self.thumbnail + except AttributeError: + pass + + def set_embed(self: E, *, url: MaybeEmpty[Any]): """Sets the thumbnail for the embed content. This function returns the class instance to allow for fluent-style @@ -453,16 +485,10 @@ class Embed: url: :class:`str` The source URL for the thumbnail. Only HTTP(S) is supported. """ - if url is EmptyEmbed: - try: - del self._thumbnail - except AttributeError: - pass + del self.thumbnail else: - self._thumbnail = { - 'url': str(url), - } + self.thumbnail = url return self -- 2.47.3 From c8cdb275c513cdaba364903a4aa8fba7ba65c78f Mon Sep 17 00:00:00 2001 From: Arthur Jovart Date: Sat, 28 Aug 2021 23:29:49 +0200 Subject: [PATCH 2/2] Fix set_* function name --- discord/embeds.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/embeds.py b/discord/embeds.py index e2119258..25f05aef 100644 --- a/discord/embeds.py +++ b/discord/embeds.py @@ -471,7 +471,7 @@ class Embed: except AttributeError: pass - def set_embed(self: E, *, url: MaybeEmpty[Any]): + def set_thumbnail(self: E, *, url: MaybeEmpty[Any]): """Sets the thumbnail for the embed content. This function returns the class instance to allow for fluent-style -- 2.47.3