aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-10-13 06:16:29 -0400
committerRapptz <[email protected]>2015-10-13 06:16:29 -0400
commit0b270442527564c37594705d35ed198ea749d78b (patch)
treec3f2ce9b360f472c0fde347a70b90b6efe13c798
parentDocumentation cleanup. (diff)
downloaddiscord.py-0b270442527564c37594705d35ed198ea749d78b.tar.xz
discord.py-0b270442527564c37594705d35ed198ea749d78b.zip
Client.send_file now properly closes the file-object.
-rw-r--r--discord/client.py72
1 files changed, 33 insertions, 39 deletions
diff --git a/discord/client.py b/discord/client.py
index b9920793..c144e742 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -408,6 +408,24 @@ class Client(object):
return m.group(1)
return None
+ def _resolve_destination(self, destination):
+ if isinstance(destination, Channel) or isinstance(destination, PrivateChannel):
+ return (destination.id, destination.is_private)
+ elif isinstance(destination, User):
+ found = utils.find(lambda pm: pm.user == destination, self.private_channels)
+ if found is None:
+ # Couldn't find the user, so start a PM with them first.
+ self.start_private_message(destination)
+ channel_id = self.private_channels[-1].id
+ return (channel_id, True)
+ else:
+ return (found.id, True)
+ elif isinstance(destination, str):
+ channel_id = destination
+ return (destination, True)
+ else:
+ raise InvalidDestination('Destination must be Channel, PrivateChannel, User, or str')
+
def on_error(self, event_method, *args, **kwargs):
msg = 'Caught exception in {} with args (*{}, **{})'
log.exception(msg.format(event_method, args, kwargs))
@@ -514,23 +532,7 @@ class Client(object):
:return: The :class:`Message` sent or None if error occurred.
"""
- channel_id = ''
- is_private_message = True
- if isinstance(destination, Channel) or isinstance(destination, PrivateChannel):
- channel_id = destination.id
- is_private_message = destination.is_private
- elif isinstance(destination, User):
- found = utils.find(lambda pm: pm.user == destination, self.private_channels)
- if found is None:
- # Couldn't find the user, so start a PM with them first.
- self.start_private_message(destination)
- channel_id = self.private_channels[-1].id
- else:
- channel_id = found.id
- elif isinstance(destination, str):
- channel_id = destination
- else:
- raise InvalidDestination('Destination must be Channel, PrivateChannel, User, or str')
+ channel_id, is_private_message = self._resolve_destination(destination)
content = str(content)
mentions = self._resolve_mentions(content, mentions)
@@ -559,34 +561,26 @@ class Client(object):
def send_file(self, destination, filename):
"""Sends a message to the destination given with the file given.
- The destination could be a :class:`Channel` or a :class:`PrivateChannel`. For convenience
- it could also be a :class:`User`. If it's a :class:`User` or :class:`PrivateChannel` then it
- sends the message via private message, otherwise it sends the message to the channel.
+ The destination parameter follows the same rules as :meth:`send_message`.
+
+ Note that this requires proper permissions in order to work.
:param destination: The location to send the message.
- :param filename: The relative file path to the file to be sent.
+ :param filename: The file to send.
+ :return: The :class:`Message` sent or None if an error occurred.
"""
- channel_id = ''
- is_private_message = True
- if isinstance(destination, Channel) or isinstance(destination, PrivateChannel):
- channel_id = destination.id
- is_private_message = destination.is_private
- elif isinstance(destination, User):
- found = utils.find(lambda pm: pm.user == destination, self.private_channels)
- if found is None:
- # Couldn't find the user, so start a PM with them first.
- self.start_private_message(destination)
- channel_id = self.private_channels[-1].id
- else:
- channel_id = found.id
- else:
- raise InvalidDestination('Destination must be Channel, PrivateChannel, or User')
-
+ channel_id, is_private_message = self._resolve_destination(destination)
+
url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
+ response = None
+
+ with open(filename, 'rb') as f:
+ files = {
+ 'file': (filename, f)
+ }
+ response = requests.post(url, files=files, headers=self.headers)
- response = requests.post(url, files={'file' : (filename, open(filename, 'rb'))}, headers=self.headers)
-
if is_response_successful(response):
data = response.json()
log.debug(request_success_log.format(name='send_file', response=response, json=response.text, data=filename))