mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-09-06 09:56:09 +00:00
Add support for multiple file attachments.
This is a breaking change. No longer does Messageable.send have a filename keyword argument, instead this is all handled through the discord.File model. To upload many files you must specify a list of discord.File objects.
This commit is contained in:
73
discord/file.py
Normal file
73
discord/file.py
Normal file
@ -0,0 +1,73 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2017 Rapptz
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
import os.path
|
||||
|
||||
class File:
|
||||
"""A parameter object used for :meth:`abc.Messageable.send`
|
||||
for sending file objects.
|
||||
|
||||
Attributes
|
||||
-----------
|
||||
fp: Union[str, BinaryIO]
|
||||
A file-like object opened in binary mode and read mode
|
||||
or a filename representing a file in the hard drive to
|
||||
open.
|
||||
|
||||
.. note::
|
||||
|
||||
If the file-like object passed is opened via ``open`` then the
|
||||
modes 'rb' should be used.
|
||||
|
||||
To pass binary data, consider usage of ``io.BytesIO``.
|
||||
|
||||
filename: Optional[str]
|
||||
The filename to display when uploading to Discord.
|
||||
If this is not given then it defaults to ``fp.name`` or if ``fp`` is
|
||||
a string then the ``filename`` will default to the string given.
|
||||
"""
|
||||
|
||||
__slots__ = ('fp', 'filename', '_true_fp')
|
||||
|
||||
def __init__(self, fp, filename=None):
|
||||
self.fp = fp
|
||||
self._true_fp = None
|
||||
|
||||
if filename is None:
|
||||
if isinstance(fp, str):
|
||||
_, self.filename = os.path.split(fp)
|
||||
else:
|
||||
self.filename = getattr(fp, 'name', None)
|
||||
|
||||
def open_file(self):
|
||||
fp = self.fp
|
||||
if isinstance(fp, str):
|
||||
self._true_fp = fp = open(fp, 'rb')
|
||||
return fp
|
||||
|
||||
def close(self):
|
||||
if self._true_fp:
|
||||
self._true_fp.close()
|
Reference in New Issue
Block a user