Browse Source

Support path-like objects in Attachment.save.

Fix #1958
pull/1994/head
Rapptz 6 years ago
parent
commit
6ffd079cb9
  1. 11
      discord/message.py

11
discord/message.py

@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE.
import asyncio import asyncio
import datetime import datetime
import re import re
import io
from . import utils from . import utils
from .reaction import Reaction from .reaction import Reaction
@ -84,7 +85,7 @@ class Attachment:
Parameters Parameters
----------- -----------
fp: Union[BinaryIO, str] fp: Union[BinaryIO, :class:`os.PathLike`]
The file-like object to save this attachment to or the filename The file-like object to save this attachment to or the filename
to use. If a filename is passed then a file is created with that to use. If a filename is passed then a file is created with that
filename and used instead. filename and used instead.
@ -112,14 +113,14 @@ class Attachment:
""" """
url = self.proxy_url if use_cached else self.url url = self.proxy_url if use_cached else self.url
data = await self._http.get_attachment(url) data = await self._http.get_attachment(url)
if isinstance(fp, str): if isinstance(fp, io.IOBase) and fp.writable():
with open(fp, 'wb') as f:
return f.write(data)
else:
written = fp.write(data) written = fp.write(data)
if seek_begin: if seek_begin:
fp.seek(0) fp.seek(0)
return written return written
else:
with open(fp, 'wb') as f:
return f.write(data)
class Message: class Message:
r"""Represents a message from Discord. r"""Represents a message from Discord.

Loading…
Cancel
Save