From 7e6a212f9f54bdd6d818a2099a07e7f1bc2f6d1f Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Tue, 13 Aug 2024 23:28:45 +0200 Subject: [PATCH 1/8] implement local files for more embed methods --- disnake/embeds.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/disnake/embeds.py b/disnake/embeds.py index 1866d8d7eb..3da9a2e010 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -106,7 +106,7 @@ class _EmbedAuthorProxy(Sized, Protocol): icon_url: Optional[str] proxy_icon_url: Optional[str] - _FileKey = Literal["image", "thumbnail"] + _FileKey = Literal["image", "thumbnail", "footer", "author"] class Embed: @@ -385,12 +385,20 @@ def footer(self) -> _EmbedFooterProxy: """ return cast("_EmbedFooterProxy", EmbedProxy(self._footer)) - def set_footer(self, *, text: Any, icon_url: Optional[Any] = None) -> Self: + def set_footer( + self, *, text: Any, icon_url: Optional[Any] = MISSING, file: File = MISSING + ) -> Self: """Sets the footer for the embed content. This function returns the class instance to allow for fluent-style chaining. + Exactly one of ``icon_url`` or ``file`` should be passed at a time, if passed. + + .. warning:: + Passing a :class:`disnake.File` object will make the embed not + reusable. + Parameters ---------- text: :class:`str` @@ -401,13 +409,18 @@ def set_footer(self, *, text: Any, icon_url: Optional[Any] = None) -> Self: icon_url: Optional[:class:`str`] The URL of the footer icon. Only HTTP(S) is supported. + file: :clas:`File` + The file to use as the image. + + .. versionadded:: 2.10 """ self._footer = { "text": str(text), } - if icon_url is not None: - self._footer["icon_url"] = str(icon_url) + result = self._handle_resource(icon_url, file, key="footer") + if result is not None: + self._footer["icon_url"] = result return self @@ -564,13 +577,20 @@ def set_author( *, name: Any, url: Optional[Any] = None, - icon_url: Optional[Any] = None, + icon_url: Optional[Any] = MISSING, + file: File = MISSING, ) -> Self: """Sets the author for the embed content. This function returns the class instance to allow for fluent-style chaining. + Exactly one of ``icon_url`` or ``file`` should be passed at a time, if passed. + + .. warning:: + Passing a :class:`disnake.File` object will make the embed not + reusable. + Parameters ---------- name: :class:`str` @@ -579,6 +599,10 @@ def set_author( The URL for the author. icon_url: Optional[:class:`str`] The URL of the author icon. Only HTTP(S) is supported. + file: :clas:`File` + The file to use as the image. + + .. versionadded:: 2.10 """ self._author = { "name": str(name), @@ -587,8 +611,9 @@ def set_author( if url is not None: self._author["url"] = str(url) - if icon_url is not None: - self._author["icon_url"] = str(icon_url) + result = self._handle_resource(icon_url, file, key="author") + if result is not None: + self._author["icon_url"] = result return self From cbcccd91133b5a00399e3e5ba3ef1be688594594 Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Tue, 13 Aug 2024 23:39:32 +0200 Subject: [PATCH 2/8] add changelog --- changelog/1184.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/1184.feature.rst diff --git a/changelog/1184.feature.rst b/changelog/1184.feature.rst new file mode 100644 index 0000000000..243bf38dea --- /dev/null +++ b/changelog/1184.feature.rst @@ -0,0 +1 @@ +Add the possibility to pass :class:`disnake.File` objects to :meth:`~Embed.set_author` and :meth:`~Embed.set_icon`. From 6c0038bd902805f1ca8fe4ba6b1d42ce0bcd7aee Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:00:47 +0200 Subject: [PATCH 3/8] mention uniqueness constraint for File names --- disnake/embeds.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/disnake/embeds.py b/disnake/embeds.py index 3da9a2e010..7a7ec8525b 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -399,6 +399,11 @@ def set_footer( Passing a :class:`disnake.File` object will make the embed not reusable. + .. note:: + If used with the other ``set_*`` methods you must ensure + that the :attr:`.File.filename` is different than the other file(s) + that you are passing. + Parameters ---------- text: :class:`str` @@ -470,6 +475,11 @@ def set_image(self, url: Optional[Any] = MISSING, *, file: File = MISSING) -> Se Passing a :class:`disnake.File` object will make the embed not reusable. + .. note:: + If used with the other ``set_*`` methods you must ensure + that the :attr:`.File.filename` is different than the other file(s) + that you are passing. + .. versionchanged:: 1.4 Passing ``None`` removes the image. @@ -521,6 +531,11 @@ def set_thumbnail(self, url: Optional[Any] = MISSING, *, file: File = MISSING) - Passing a :class:`disnake.File` object will make the embed not reusable. + .. note:: + If used with the other ``set_*`` methods you must ensure + that the :attr:`.File.filename` is different than the other file(s) + that you are passing. + .. versionchanged:: 1.4 Passing ``None`` removes the thumbnail. @@ -591,6 +606,11 @@ def set_author( Passing a :class:`disnake.File` object will make the embed not reusable. + .. note:: + If used with the other ``set_*`` methods you must ensure + that the :attr:`.File.filename` is different than the other file(s) + that you are passing. + Parameters ---------- name: :class:`str` From 70e43bd5783a3fcbfbd4e9b64f088d7e2361a2ba Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:11:00 +0200 Subject: [PATCH 4/8] add overloads --- disnake/embeds.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/disnake/embeds.py b/disnake/embeds.py index 7a7ec8525b..312edce2c9 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -385,6 +385,14 @@ def footer(self) -> _EmbedFooterProxy: """ return cast("_EmbedFooterProxy", EmbedProxy(self._footer)) + @overload + def set_footer(self, *, text: Any, icon_url: Optional[Any]) -> Self: + ... + + @overload + def set_footer(self, *, text: Any, file: File) -> Self: + ... + def set_footer( self, *, text: Any, icon_url: Optional[Any] = MISSING, file: File = MISSING ) -> Self: @@ -587,6 +595,14 @@ def author(self) -> _EmbedAuthorProxy: """ return cast("_EmbedAuthorProxy", EmbedProxy(self._author)) + @overload + def set_author(self, *, name: Any, url: Optional[Any], icon_url: Optional[Any]) -> Self: + ... + + @overload + def set_author(self, *, name: Any, url: Optional[Any], file: File) -> Self: + ... + def set_author( self, *, From 59dd7006dbf31b4f3788e2785bde66fe827fed14 Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:13:04 +0200 Subject: [PATCH 5/8] fix overloads --- disnake/embeds.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/disnake/embeds.py b/disnake/embeds.py index 312edce2c9..724cc5967b 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -386,11 +386,11 @@ def footer(self) -> _EmbedFooterProxy: return cast("_EmbedFooterProxy", EmbedProxy(self._footer)) @overload - def set_footer(self, *, text: Any, icon_url: Optional[Any]) -> Self: + def set_footer(self, *, text: Any, icon_url: Optional[Any] = ...) -> Self: ... @overload - def set_footer(self, *, text: Any, file: File) -> Self: + def set_footer(self, *, text: Any, file: File = ...) -> Self: ... def set_footer( @@ -596,11 +596,13 @@ def author(self) -> _EmbedAuthorProxy: return cast("_EmbedAuthorProxy", EmbedProxy(self._author)) @overload - def set_author(self, *, name: Any, url: Optional[Any], icon_url: Optional[Any]) -> Self: + def set_author( + self, *, name: Any, url: Optional[Any] = ..., icon_url: Optional[Any] = ... + ) -> Self: ... @overload - def set_author(self, *, name: Any, url: Optional[Any], file: File) -> Self: + def set_author(self, *, name: Any, url: Optional[Any] = ..., file: File = ...) -> Self: ... def set_author( From b5bc743be00d03bab2e22cff8c80d8a8c5170a6f Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:34:16 +0200 Subject: [PATCH 6/8] make tests pass --- disnake/embeds.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/disnake/embeds.py b/disnake/embeds.py index 724cc5967b..8e20cafc43 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -431,7 +431,7 @@ def set_footer( "text": str(text), } - result = self._handle_resource(icon_url, file, key="footer") + result = self._handle_resource(icon_url, file, key="footer", strict=False) if result is not None: self._footer["icon_url"] = result @@ -649,7 +649,9 @@ def set_author( if url is not None: self._author["url"] = str(url) - result = self._handle_resource(icon_url, file, key="author") + result = self._handle_resource( + icon_url if icon_url else None, file, key="author", strict=False + ) if result is not None: self._author["icon_url"] = result @@ -884,9 +886,15 @@ def get_default_colour(cls) -> Optional[Colour]: get_default_color = get_default_colour - def _handle_resource(self, url: Optional[Any], file: File, *, key: _FileKey) -> Optional[str]: - if not (url is MISSING) ^ (file is MISSING): - raise TypeError("Exactly one of url or file must be provided") + def _handle_resource( + self, url: Optional[Any], file: File, *, key: _FileKey, strict: bool = True + ) -> Optional[str]: + if strict: + if not (url is MISSING) ^ (file is MISSING): + raise TypeError("Exactly one of url or file must be provided") + else: + if url is not MISSING and file is not MISSING: + raise TypeError("Only one of url or file must be provided, not both.") if file: if file.filename is None: From 297e71c038b59a0b782ab4d9e3f16ec20720bc96 Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:48:10 +0200 Subject: [PATCH 7/8] fix docs --- disnake/embeds.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/disnake/embeds.py b/disnake/embeds.py index 8e20cafc43..337c7cb8d0 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -422,7 +422,7 @@ def set_footer( icon_url: Optional[:class:`str`] The URL of the footer icon. Only HTTP(S) is supported. - file: :clas:`File` + file: :class:`File` The file to use as the image. .. versionadded:: 2.10 @@ -637,7 +637,7 @@ def set_author( The URL for the author. icon_url: Optional[:class:`str`] The URL of the author icon. Only HTTP(S) is supported. - file: :clas:`File` + file: :class:`File` The file to use as the image. .. versionadded:: 2.10 From 81c60ec5074acb708783197ba7902f59283cd712 Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Sat, 14 Sep 2024 09:14:41 +0200 Subject: [PATCH 8/8] apply nit name suggestion --- disnake/embeds.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/disnake/embeds.py b/disnake/embeds.py index 337c7cb8d0..f0b0b8b07a 100644 --- a/disnake/embeds.py +++ b/disnake/embeds.py @@ -431,7 +431,7 @@ def set_footer( "text": str(text), } - result = self._handle_resource(icon_url, file, key="footer", strict=False) + result = self._handle_resource(icon_url, file, key="footer", required=False) if result is not None: self._footer["icon_url"] = result @@ -650,7 +650,7 @@ def set_author( self._author["url"] = str(url) result = self._handle_resource( - icon_url if icon_url else None, file, key="author", strict=False + icon_url if icon_url else None, file, key="author", required=False ) if result is not None: self._author["icon_url"] = result @@ -887,9 +887,9 @@ def get_default_colour(cls) -> Optional[Colour]: get_default_color = get_default_colour def _handle_resource( - self, url: Optional[Any], file: File, *, key: _FileKey, strict: bool = True + self, url: Optional[Any], file: File, *, key: _FileKey, required: bool = True ) -> Optional[str]: - if strict: + if required: if not (url is MISSING) ^ (file is MISSING): raise TypeError("Exactly one of url or file must be provided") else: