aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleyoshi4 <[email protected]>2015-10-11 21:51:10 -0500
committerlittleyoshi4 <[email protected]>2015-10-11 21:51:10 -0500
commit1b6fe0466d63383b7ad737a30b2b25fc90c2b0cd (patch)
tree4db0184a7890418e3693cea18735fcdbdc2cdf90
parentClient.register now takes an invite URL or class. (diff)
downloaddiscord.py-1b6fe0466d63383b7ad737a30b2b25fc90c2b0cd.tar.xz
discord.py-1b6fe0466d63383b7ad737a30b2b25fc90c2b0cd.zip
Added send_file method.
-rw-r--r--discord/client.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/discord/client.py b/discord/client.py
index f01a05fd..51f452d9 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -553,6 +553,46 @@ class Client(object):
else:
log.error(request_logging_format.format(name='send_message', response=response))
+ 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.
+
+ :param destination: The location to send the message.
+ :param filename: The relative file path to the file to be sent.
+ """
+
+ 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')
+
+ url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
+
+ 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))
+ channel = self.get_channel(data.get('channel_id'))
+ message = Message(channel=channel, **data)
+ return message
+ else:
+ log.error(request_logging_format.format(name='send_file', response=response))
+
def delete_message(self, message):
"""Deletes a :class:`Message`.