Browse Source

Add on_message_edit event.

pull/1/head
Rapptz 10 years ago
parent
commit
ef7dd42f28
  1. 21
      discord/client.py
  2. 8
      docs/api.rst
  3. 16
      examples/edits.py

21
discord/client.py

@ -93,7 +93,8 @@ class Client(object):
'on_error': _null_event,
'on_response': _null_event,
'on_message': _null_event,
'on_message_delete': _null_event
'on_message_delete': _null_event,
'on_message_edit': _null_event
}
self.ws = WebSocketClient(endpoints.WEBSOCKET_HUB, protocols=['http-only', 'chat'])
@ -114,6 +115,9 @@ class Client(object):
'authorization': self.token,
}
def _get_message(self, msg_id):
return next((m for m in self.messages if m.id == msg_id), None)
def _received_message(self, msg):
response = json.loads(str(msg))
if response.get('op') != 0:
@ -152,10 +156,23 @@ class Client(object):
elif event == 'MESSAGE_DELETE':
channel = self.get_channel(data.get('channel_id'))
message_id = data.get('id')
found = next((m for m in self.messages if m.id == message_id), None)
found = self._get_message(message_id)
if found is not None:
self.events['on_message_delete'](found)
self.messages.remove(found)
elif event == 'MESSAGE_UPDATE':
# {u'edited_timestamp': u'2015-08-22T01:19:23.002892+00:00', u'attachments': [], u'channel_id': u'81840769509363712', u'tts': False, u'timestamp': u'2015-08-22T01:19:20.377000+00:00', u'author': {u'username': u'Danny', u'discriminator': u'9173', u'id': u'80088516616269824', u'avatar': u'd9dab18704d8cdcf5a022f9e913420fa'}, u'content': u'goodbye', u'embeds': [], u'mention_everyone': False, u'mentions': [], u'id': u'84456339153092608'}
older_message = self._get_message(data.get('id'))
if older_message is not None:
message = Message(channel=older_message.channel, **data)
self.events['on_message_edit'](older_message, message)
older_message.edited_timestamp = message.edited_timestamp
else:
# if we couldn't find the message in our cache, just add it to the list
channel = self.get_channel(data.get('channel_id'))
message = Message(channel=channel, **data)
self.messages.append(message)
def _opened(self):
print('Opened at {}'.format(int(time.time())))

8
docs/api.rst

@ -39,10 +39,16 @@ This page outlines the different types of events listened to by :meth:`Client.ev
:param response: The received message response after gone through ``json.loads``.
.. function:: on_message_delete(message)
.. function:: on_message_edit(before, after)
Called when a message is deleted from any given server.
Called when a message is deleted or edited from any given server. If the message is not found in the
:attr:`Client.messages` cache, then these events will not be called. This happens if the message
is too old or the client is participating in high traffic servers. To fix this, increase
the ``max_length`` option of :class:`Client`.
:param message: A :class:`Message` of the deleted message.
:param before: A :class:`Message` of the previous version of the message.
:param after: A :class:`Message` of the current version of the message.
Data Classes

16
examples/edits.py

@ -0,0 +1,16 @@
import discord
client = discord.Client()
client.login('email', 'password')
@client.event
def on_ready():
print('Connected!')
print('Username: ' + client.user.name)
print('ID: ' + client.user.id)
@client.event
def on_message_edit(before, after):
client.send_message(after.channel, '**{}** edited their message:\n{}'.format(after.author.name, before.content))
client.run()
Loading…
Cancel
Save