Browse Source

Fix issue with some MESSAGE_UPDATE events.

pull/1/head v0.2.1
Rapptz 10 years ago
parent
commit
de3bce2b32
  1. 4
      discord/__init__.py
  2. 23
      discord/client.py
  3. 13
      discord/message.py

4
discord/__init__.py

@ -15,8 +15,8 @@ __title__ = 'discord'
__author__ = 'Rapptz' __author__ = 'Rapptz'
__license__ = 'MIT' __license__ = 'MIT'
__copyright__ = 'Copyright 2015 Rapptz' __copyright__ = 'Copyright 2015 Rapptz'
__version__ = '0.2.0' __version__ = '0.2.1'
__build__ = 0x002000 __build__ = 0x002010
from client import Client from client import Client
from user import User from user import User

23
discord/client.py

@ -25,7 +25,7 @@ DEALINGS IN THE SOFTWARE.
""" """
import requests import requests
import json, re, time import json, re, time, copy
import endpoints import endpoints
from collections import deque from collections import deque
from threading import Timer from threading import Timer
@ -179,14 +179,21 @@ class Client(object):
elif event == 'MESSAGE_UPDATE': elif event == 'MESSAGE_UPDATE':
older_message = self._get_message(data.get('id')) older_message = self._get_message(data.get('id'))
if older_message is not None: if older_message is not None:
message = Message(channel=older_message.channel, **data) # create a copy of the new message
self._invoke_event('on_message_edit', older_message, message) message = copy.deepcopy(older_message)
older_message.edited_timestamp = message.edited_timestamp # update the new update
for attr in data:
if attr == 'channel_id':
continue
value = data[attr]
if 'time' in attr:
setattr(message, attr, message._parse_time(value))
else: else:
# if we couldn't find the message in our cache, just add it to the list setattr(message, attr, value)
channel = self.get_channel(data.get('channel_id')) self._invoke_event('on_message_edit', older_message, message)
message = Message(channel=channel, **data) # update the older message
self.messages.append(message) older_message = message
elif event == 'PRESENCE_UPDATE': elif event == 'PRESENCE_UPDATE':
guild_id = data.get('guild_id') guild_id = data.get('guild_id')
server = next((s for s in self.servers if s.id == guild_id), None) server = next((s for s in self.servers if s.id == guild_id), None)

13
discord/message.py

@ -65,6 +65,9 @@ class Message(object):
.. attribute:: id .. attribute:: id
The message ID. The message ID.
.. attribute:: attachments
An array of attachments given to a message.
""" """
def __init__(self, edited_timestamp, timestamp, tts, content, mention_everyone, mentions, embeds, attachments, id, channel, author, **kwargs): def __init__(self, edited_timestamp, timestamp, tts, content, mention_everyone, mentions, embeds, attachments, id, channel, author, **kwargs):
@ -72,13 +75,11 @@ class Message(object):
# we can use this to our advantage to use strptime instead of a complicated parsing routine. # we can use this to our advantage to use strptime instead of a complicated parsing routine.
# example timestamp: 2015-08-21T12:03:45.782000+00:00 # example timestamp: 2015-08-21T12:03:45.782000+00:00
# sometimes the .%f modifier is missing # sometimes the .%f modifier is missing
time_format = "%Y-%m-%dT%H:%M:%S.%f+00:00"
self.edited_timestamp = None self.edited_timestamp = None
if edited_timestamp is not None: if edited_timestamp is not None:
temp = edited_timestamp.replace('+00:00', '') self.edited_timestamp = self._parse_time(edited_timestamp)
self.edited_timestamp = datetime.datetime(*map(int, re.split(r'[^\d]', temp)))
self.timestamp = datetime.datetime(*map(int, re.split(r'[^\d]', timestamp.replace('+00:00', '')))) self.timestamp = self._parse_time(timestamp)
self.tts = tts self.tts = tts
self.content = content self.content = content
self.mention_everyone = mention_everyone self.mention_everyone = mention_everyone
@ -87,6 +88,8 @@ class Message(object):
self.channel = channel self.channel = channel
self.author = User(**author) self.author = User(**author)
self.mentions = [User(**mention) for mention in mentions] self.mentions = [User(**mention) for mention in mentions]
self.attachments = attachments
def _parse_time(self, time_string):
return datetime.datetime(*map(int, re.split(r'[^\d]', time_string.replace('+00:00', ''))))

Loading…
Cancel
Save