From b102ac4b2aa0c1cede1f9462edebd851c25128d3 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 21 Aug 2015 22:53:47 -0400 Subject: [PATCH] Add the ability to edit messages. --- discord/client.py | 44 ++++++++++++++++++++++++++++++++++++-------- examples/edits.py | 8 ++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/discord/client.py b/discord/client.py index bcd151770..f81db1c12 100644 --- a/discord/client.py +++ b/discord/client.py @@ -118,6 +118,14 @@ class Client(object): def _get_message(self, msg_id): return next((m for m in self.messages if m.id == msg_id), None) + def _resolve_mentions(self, content, mentions): + if isinstance(mentions, list): + return [user.id for user in mentions] + elif mentions == True: + return re.findall(r'@<(\d+)>', content) + else: + return [] + def _received_message(self, msg): response = json.loads(str(msg)) if response.get('op') != 0: @@ -258,13 +266,7 @@ class Client(object): raise InvalidDestination('Destination must be Channel, PrivateChannel, or User') content = str(content) - - if isinstance(mentions, list): - mentions = [user.id for user in mentions] - elif mentions == True: - mentions = re.findall(r'@<(\d+)>', content) - else: - mentions = [] + mentions = self._resolve_mentions(content, mentions) url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id) payload = { @@ -278,7 +280,7 @@ class Client(object): if response.status_code == 200: data = response.json() channel = self.get_channel(data.get('channel_id')) - message = Message(channel=channel, **response.json()) + message = Message(channel=channel, **data) return message def delete_message(self, message): @@ -291,6 +293,32 @@ class Client(object): url = '{}/{}/messages/{}'.format(endpoints.CHANNELS, message.channel.id, message.id) response = requests.delete(url, headers=self.headers) + def edit_message(self, message, new_content, mentions=True): + """Edits a :class:`Message` with the new message content. + + The new_content must be able to be transformed into a string via ``str(new_content)``. + + :param message: The :class:`Message` to edit. + :param new_content: The new content to replace the message with. + :param mentions: The mentions for the user. Same as :meth:`send_message`. + :return: The new edited message or None if an error occurred.""" + channel = message.channel + content = str(new_content) + + url = '{}/{}/messages/{}'.format(endpoints.CHANNELS, channel.id, message.id) + payload = { + 'content': content + } + + if not channel.is_private: + payload['mentions'] = self._resolve_mentions(content, mentions) + + response = requests.patch(url, headers=self.headers, json=payload) + if response.status_code == 200: + data = response.json() + return Message(channel=channel, **data) + + def login(self, email, password): """Logs in the user with the following credentials. diff --git a/examples/edits.py b/examples/edits.py index c1619cecb..e8233bbb0 100644 --- a/examples/edits.py +++ b/examples/edits.py @@ -1,4 +1,5 @@ import discord +import time client = discord.Client() client.login('email', 'password') @@ -9,6 +10,13 @@ def on_ready(): print('Username: ' + client.user.name) print('ID: ' + client.user.id) +@client.event +def on_message(message): + if message.content.startswith('!editme'): + msg = client.send_message(message.author, '10') + time.sleep(3) + client.edit_message(msg, '40') + @client.event def on_message_edit(before, after): client.send_message(after.channel, '**{}** edited their message:\n{}'.format(after.author.name, before.content))