Add read method to attachment objects
Refactor save to use new read method
This commit is contained in:
parent
05d4f7f962
commit
7dfaa5e9ae
@ -112,8 +112,7 @@ class Attachment:
|
|||||||
:class:`int`
|
:class:`int`
|
||||||
The number of bytes written.
|
The number of bytes written.
|
||||||
"""
|
"""
|
||||||
url = self.proxy_url if use_cached else self.url
|
data = await self.read(use_cached=use_cached)
|
||||||
data = await self._http.get_from_cdn(url)
|
|
||||||
if isinstance(fp, io.IOBase) and fp.writable():
|
if isinstance(fp, io.IOBase) and fp.writable():
|
||||||
written = fp.write(data)
|
written = fp.write(data)
|
||||||
if seek_begin:
|
if seek_begin:
|
||||||
@ -123,6 +122,42 @@ class Attachment:
|
|||||||
with open(fp, 'wb') as f:
|
with open(fp, 'wb') as f:
|
||||||
return f.write(data)
|
return f.write(data)
|
||||||
|
|
||||||
|
async def read(self, *, use_cached=False):
|
||||||
|
"""|coro|
|
||||||
|
|
||||||
|
Retrieves the content of this attachment as a :class:`bytes` object.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
-----------
|
||||||
|
use_cached: :class:`bool`
|
||||||
|
Whether to use :attr:`proxy_url` rather than :attr:`url` when downloading
|
||||||
|
the attachment. This will allow attachments to be saved after deletion
|
||||||
|
more often, compared to the regular URL which is generally deleted right
|
||||||
|
after the message is deleted. Note that this can still fail to download
|
||||||
|
deleted attachments if too much time has passed and it does not work
|
||||||
|
on some type of attachments.
|
||||||
|
|
||||||
|
.. versionadded:: 1.1.0
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
HTTPException
|
||||||
|
Downloading the attachment failed.
|
||||||
|
Forbidden
|
||||||
|
You do not have permissions to access this attachment
|
||||||
|
NotFound
|
||||||
|
The attachment was deleted.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
:class:`bytes`
|
||||||
|
The contents of the attachment.
|
||||||
|
"""
|
||||||
|
url = self.proxy_url if use_cached else self.url
|
||||||
|
data = await self._http.get_from_cdn(url)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class Message:
|
class Message:
|
||||||
r"""Represents a message from Discord.
|
r"""Represents a message from Discord.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user