mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 20:28:38 +00:00
Add the ability to edit messages.
This commit is contained in:
parent
ef7dd42f28
commit
b102ac4b2a
@ -118,6 +118,14 @@ class Client(object):
|
|||||||
def _get_message(self, msg_id):
|
def _get_message(self, msg_id):
|
||||||
return next((m for m in self.messages if m.id == msg_id), None)
|
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):
|
def _received_message(self, msg):
|
||||||
response = json.loads(str(msg))
|
response = json.loads(str(msg))
|
||||||
if response.get('op') != 0:
|
if response.get('op') != 0:
|
||||||
@ -258,13 +266,7 @@ class Client(object):
|
|||||||
raise InvalidDestination('Destination must be Channel, PrivateChannel, or User')
|
raise InvalidDestination('Destination must be Channel, PrivateChannel, or User')
|
||||||
|
|
||||||
content = str(content)
|
content = str(content)
|
||||||
|
mentions = self._resolve_mentions(content, mentions)
|
||||||
if isinstance(mentions, list):
|
|
||||||
mentions = [user.id for user in mentions]
|
|
||||||
elif mentions == True:
|
|
||||||
mentions = re.findall(r'@<(\d+)>', content)
|
|
||||||
else:
|
|
||||||
mentions = []
|
|
||||||
|
|
||||||
url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
|
url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
|
||||||
payload = {
|
payload = {
|
||||||
@ -278,7 +280,7 @@ class Client(object):
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
channel = self.get_channel(data.get('channel_id'))
|
channel = self.get_channel(data.get('channel_id'))
|
||||||
message = Message(channel=channel, **response.json())
|
message = Message(channel=channel, **data)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def delete_message(self, message):
|
def delete_message(self, message):
|
||||||
@ -291,6 +293,32 @@ class Client(object):
|
|||||||
url = '{}/{}/messages/{}'.format(endpoints.CHANNELS, message.channel.id, message.id)
|
url = '{}/{}/messages/{}'.format(endpoints.CHANNELS, message.channel.id, message.id)
|
||||||
response = requests.delete(url, headers=self.headers)
|
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):
|
def login(self, email, password):
|
||||||
"""Logs in the user with the following credentials.
|
"""Logs in the user with the following credentials.
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import discord
|
import discord
|
||||||
|
import time
|
||||||
|
|
||||||
client = discord.Client()
|
client = discord.Client()
|
||||||
client.login('email', 'password')
|
client.login('email', 'password')
|
||||||
@ -9,6 +10,13 @@ def on_ready():
|
|||||||
print('Username: ' + client.user.name)
|
print('Username: ' + client.user.name)
|
||||||
print('ID: ' + client.user.id)
|
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
|
@client.event
|
||||||
def on_message_edit(before, after):
|
def on_message_edit(before, after):
|
||||||
client.send_message(after.channel, '**{}** edited their message:\n{}'.format(after.author.name, before.content))
|
client.send_message(after.channel, '**{}** edited their message:\n{}'.format(after.author.name, before.content))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user