Browse Source

Add filename and description kwargs for to_file

pull/7887/head
Jonah Lawrence 3 years ago
committed by GitHub
parent
commit
862eba1e59
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      discord/asset.py
  2. 23
      discord/message.py

22
discord/asset.py

@ -125,7 +125,13 @@ class AssetMixin:
with open(fp, 'wb') as f: with open(fp, 'wb') as f:
return f.write(data) return f.write(data)
async def to_file(self, *, spoiler: bool = False) -> File: async def to_file(
self,
*,
filename: Optional[str] = MISSING,
description: Optional[str] = None,
spoiler: bool = False,
) -> File:
"""|coro| """|coro|
Converts the asset into a :class:`File` suitable for sending via Converts the asset into a :class:`File` suitable for sending via
@ -135,6 +141,11 @@ class AssetMixin:
Parameters Parameters
----------- -----------
filename: Optional[:class:`str`]
The filename of the file. If not provided, then the filename from
the asset's URL is used.
description: Optional[:class:`str`]
The description for the file.
spoiler: :class:`bool` spoiler: :class:`bool`
Whether the file is a spoiler. Whether the file is a spoiler.
@ -142,12 +153,12 @@ class AssetMixin:
------ ------
DiscordException DiscordException
The asset does not have an associated state. The asset does not have an associated state.
ValueError
The asset is a unicode emoji.
TypeError TypeError
The asset is a sticker with lottie type. The asset is a sticker with lottie type.
HTTPException HTTPException
Downloading the asset failed. Downloading the asset failed.
Forbidden
You do not have permissions to access this asset.
NotFound NotFound
The asset was deleted. The asset was deleted.
@ -158,9 +169,8 @@ class AssetMixin:
""" """
data = await self.read() data = await self.read()
url = yarl.URL(self.url) file_filename = filename if filename is not MISSING else yarl.URL(self.url).name
_, _, filename = url.path.rpartition('/') return File(io.BytesIO(data), filename=file_filename, description=description, spoiler=spoiler)
return File(io.BytesIO(data), filename=filename, spoiler=spoiler)
class Asset(AssetMixin): class Asset(AssetMixin):

23
discord/message.py

@ -303,7 +303,14 @@ class Attachment(Hashable):
data = await self._http.get_from_cdn(url) data = await self._http.get_from_cdn(url)
return data return data
async def to_file(self, *, use_cached: bool = False, spoiler: bool = False) -> File: async def to_file(
self,
*,
filename: Optional[str] = MISSING,
description: Optional[str] = MISSING,
use_cached: bool = False,
spoiler: bool = False,
) -> File:
"""|coro| """|coro|
Converts the attachment into a :class:`File` suitable for sending via Converts the attachment into a :class:`File` suitable for sending via
@ -313,6 +320,16 @@ class Attachment(Hashable):
Parameters Parameters
----------- -----------
filename: Optional[:class:`str`]
The filename to use for the file. If not specified then the filename
of the attachment is used instead.
.. versionadded:: 2.0
description: Optional[:class:`str`]
The description to use for the file. If not specified then the
description of the attachment is used instead.
.. versionadded:: 2.0
use_cached: :class:`bool` use_cached: :class:`bool`
Whether to use :attr:`proxy_url` rather than :attr:`url` when downloading Whether to use :attr:`proxy_url` rather than :attr:`url` when downloading
the attachment. This will allow attachments to be saved after deletion the attachment. This will allow attachments to be saved after deletion
@ -343,7 +360,9 @@ class Attachment(Hashable):
""" """
data = await self.read(use_cached=use_cached) data = await self.read(use_cached=use_cached)
return File(io.BytesIO(data), filename=self.filename, description=self.description, spoiler=spoiler) file_filename = filename if filename is not MISSING else self.filename
file_description = description if description is not MISSING else self.description
return File(io.BytesIO(data), filename=file_filename, description=file_description, spoiler=spoiler)
def to_dict(self) -> AttachmentPayload: def to_dict(self) -> AttachmentPayload:
result: AttachmentPayload = { result: AttachmentPayload = {

Loading…
Cancel
Save