aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-08-21 22:53:47 -0400
committerRapptz <[email protected]>2015-08-21 22:54:12 -0400
commitb102ac4b2aa0c1cede1f9462edebd851c25128d3 (patch)
tree2c143fd02c23f8db902ee860a4d31ad8600d6dc3
parentAdd on_message_edit event. (diff)
downloaddiscord.py-b102ac4b2aa0c1cede1f9462edebd851c25128d3.tar.xz
discord.py-b102ac4b2aa0c1cede1f9462edebd851c25128d3.zip
Add the ability to edit messages.
-rw-r--r--discord/client.py44
-rw-r--r--examples/edits.py8
2 files changed, 44 insertions, 8 deletions
diff --git a/discord/client.py b/discord/client.py
index bcd15177..f81db1c1 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 c1619cec..e8233bbb 100644
--- a/examples/edits.py
+++ b/examples/edits.py
@@ -1,4 +1,5 @@
import discord
+import time
client = discord.Client()
client.login('email', 'password')
@@ -10,6 +11,13 @@ def on_ready():
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')
+
def on_message_edit(before, after):
client.send_message(after.channel, '**{}** edited their message:\n{}'.format(after.author.name, before.content))