mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-18 23:15:48 +00:00
Add to_file for assets and emojis
This commit is contained in:
parent
1372ef407c
commit
e80be19c4d
@ -29,6 +29,7 @@ import os
|
||||
from typing import Any, Literal, Optional, TYPE_CHECKING, Tuple, Union
|
||||
from .errors import DiscordException
|
||||
from . import utils
|
||||
from .file import File
|
||||
|
||||
import yarl
|
||||
|
||||
@ -92,7 +93,7 @@ class AssetMixin:
|
||||
Parameters
|
||||
----------
|
||||
fp: Union[:class:`io.BufferedIOBase`, :class:`os.PathLike`]
|
||||
The file-like object to save this attachment to or the filename
|
||||
The file-like object to save this asset to or the filename
|
||||
to use. If a filename is passed then a file is created with that
|
||||
filename and used instead.
|
||||
seek_begin: :class:`bool`
|
||||
@ -124,6 +125,43 @@ class AssetMixin:
|
||||
with open(fp, 'wb') as f:
|
||||
return f.write(data)
|
||||
|
||||
async def to_file(self, *, spoiler: bool = False) -> File:
|
||||
"""|coro|
|
||||
|
||||
Converts the asset into a :class:`File` suitable for sending via
|
||||
:meth:`abc.Messageable.send`.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
spoiler: :class:`bool`
|
||||
Whether the file is a spoiler.
|
||||
|
||||
Raises
|
||||
------
|
||||
DiscordException
|
||||
The asset does not have an associated state.
|
||||
TypeError
|
||||
The asset is a sticker with lottie type.
|
||||
HTTPException
|
||||
Downloading the asset failed.
|
||||
Forbidden
|
||||
You do not have permissions to access this asset.
|
||||
NotFound
|
||||
The asset was deleted.
|
||||
|
||||
Returns
|
||||
-------
|
||||
:class:`File`
|
||||
The asset as a file suitable for sending.
|
||||
"""
|
||||
|
||||
data = await self.read()
|
||||
url = yarl.URL(self.url)
|
||||
_, _, filename = url.path.rpartition('/')
|
||||
return File(io.BytesIO(data), filename=filename, spoiler=spoiler)
|
||||
|
||||
|
||||
class Asset(AssetMixin):
|
||||
"""Represents a CDN asset on Discord.
|
||||
|
Loading…
x
Reference in New Issue
Block a user