Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(embed): add the ability to pass disnake.File to more Embed methods #1229

Merged
merged 29 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7e6a212
implement local files for more embed methods
Snipy7374 Aug 13, 2024
cbcccd9
add changelog
Snipy7374 Aug 13, 2024
8f86b93
Merge branch 'master' into feat/embeds
Snipy7374 Aug 13, 2024
6c0038b
mention uniqueness constraint for File names
Snipy7374 Aug 26, 2024
70366dd
Merge branch 'master' into feat/embeds
Snipy7374 Aug 26, 2024
70e43bd
add overloads
Snipy7374 Aug 26, 2024
59dd700
fix overloads
Snipy7374 Aug 26, 2024
b5bc743
make tests pass
Snipy7374 Aug 26, 2024
297e71c
fix docs
Snipy7374 Aug 26, 2024
81c60ec
apply nit name suggestion
Snipy7374 Sep 14, 2024
980f1f1
Merge branch 'master' into feat/embeds
Snipy7374 Sep 14, 2024
a46c38e
Update disnake/embeds.py
Snipy7374 Nov 13, 2024
80aad9c
Update disnake/embeds.py
Snipy7374 Nov 13, 2024
eb4007a
Update disnake/embeds.py
Snipy7374 Nov 13, 2024
1b842df
Update changelog/1184.feature.rst
Snipy7374 Nov 13, 2024
25ba7e6
Merge branch 'master' into feat/embeds
Snipy7374 Nov 13, 2024
8865a33
Update disnake/embeds.py
Snipy7374 Nov 13, 2024
8c404be
feat: apply requested changes
Snipy7374 Nov 14, 2024
dbca335
Update disnake/embeds.py
Snipy7374 Nov 14, 2024
a4e1f12
Update disnake/embeds.py
Snipy7374 Nov 14, 2024
ef56e00
Update disnake/embeds.py
Snipy7374 Nov 14, 2024
fed19dc
feat: change file to icon_file
Snipy7374 Nov 14, 2024
59fe93c
feat: change notes to warnings
Snipy7374 Nov 14, 2024
cbc849d
Merge branch 'master' into feat/embeds
Snipy7374 Nov 14, 2024
d9a87cf
Merge branch 'master' into feat/embeds
Snipy7374 Nov 14, 2024
da9237e
Update disnake/embeds.py
Snipy7374 Nov 16, 2024
270b1ca
Update disnake/embeds.py
Snipy7374 Nov 16, 2024
484995f
Update disnake/embeds.py
Snipy7374 Nov 16, 2024
6e29620
Merge branch 'master' into feat/embeds
Snipy7374 Nov 16, 2024
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
1 change: 1 addition & 0 deletions changelog/1184.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the possibility to pass :class:`disnake.File` objects to :meth:`~Embed.set_author` and :meth:`~Embed.set_icon`.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
91 changes: 81 additions & 10 deletions disnake/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -385,12 +385,33 @@ def footer(self) -> _EmbedFooterProxy:
"""
return cast("_EmbedFooterProxy", EmbedProxy(self._footer))

def set_footer(self, *, text: Any, icon_url: Optional[Any] = None) -> Self:
@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
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
) -> 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.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

.. warning::
Passing a :class:`disnake.File` object will make the embed not
reusable.

.. note::
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
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.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
text: :class:`str`
Expand All @@ -401,13 +422,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: :class:`File`
The file to use as the image.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

.. 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", strict=False)
if result is not None:
self._footer["icon_url"] = result

return self

Expand Down Expand Up @@ -457,6 +483,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::
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
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.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

.. versionchanged:: 1.4
Passing ``None`` removes the image.

Expand Down Expand Up @@ -508,6 +539,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::
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
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.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

.. versionchanged:: 1.4
Passing ``None`` removes the thumbnail.

Expand Down Expand Up @@ -559,18 +595,40 @@ 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:
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
...

def set_author(
self,
*,
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.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

.. warning::
Passing a :class:`disnake.File` object will make the embed not
reusable.

.. note::
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
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.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
name: :class:`str`
Expand All @@ -579,6 +637,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: :class:`File`
The file to use as the image.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 2.10
"""
self._author = {
"name": str(name),
Expand All @@ -587,8 +649,11 @@ 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 if icon_url else None, file, key="author", strict=False
)
if result is not None:
self._author["icon_url"] = result

return self

Expand Down Expand Up @@ -821,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
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
) -> 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.")
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved

if file:
if file.filename is None:
Expand Down
Loading