Browse Source

Handle CHANNEL_UPDATE for group direct messages.

pull/276/head
Rapptz 9 years ago
parent
commit
69c506d7ae
  1. 14
      discord/channel.py
  2. 10
      discord/enums.py
  3. 18
      discord/state.py

14
discord/channel.py

@ -325,21 +325,13 @@ class PrivateChannel(Hashable):
self.id = kwargs['id']
self.is_private = True
self.type = ChannelType(kwargs['type'])
self._update_group(**kwargs)
def _update_group(self, **kwargs):
owner_id = kwargs.get('owner_id')
self.owner = None
self.icon = kwargs.get('icon')
self.name = kwargs.get('name')
self.recipients = []
for data in kwargs['recipients']:
to_add = User(**data)
if to_add.id == owner_id:
self.owner = to_add
self.recipients.append(to_add)
if owner_id == me.id:
self.owner = me
self.owner = utils.find(lambda u: u.id == owner_id, self.recipients)
def __str__(self):
return 'Direct Message with {0.name}'.format(self.user)

10
discord/enums.py

@ -73,3 +73,13 @@ class DefaultAvatar(Enum):
def __str__(self):
return self.name
def try_enum(cls, val):
"""A function that tries to turn the value into enum ``cls``.
If it fails it returns the value instead.
"""
try:
return cls(val)
except ValueError:
return val

18
discord/state.py

@ -32,7 +32,7 @@ from .channel import Channel, PrivateChannel
from .member import Member
from .role import Role
from . import utils, compat
from .enums import Status
from .enums import Status, ChannelType, try_enum
from collections import deque, namedtuple
@ -138,11 +138,13 @@ class ConnectionState:
def _add_private_channel(self, channel):
self._private_channels[channel.id] = channel
self._private_channels_by_user[channel.user.id] = channel
if channel.type is ChannelType.private:
self._private_channels_by_user[channel.user.id] = channel
def _remove_private_channel(self, channel):
self._private_channels.pop(channel.id, None)
self._private_channels_by_user.pop(channel.user.id, None)
if channel.type is ChannelType.private:
self._private_channels_by_user.pop(channel.user.id, None)
def _get_message(self, msg_id):
return utils.find(lambda m: m.id == msg_id, self.messages)
@ -289,9 +291,17 @@ class ConnectionState:
self.dispatch('channel_delete', channel)
def parse_channel_update(self, data):
channel_type = try_enum(ChannelType, data.get('type'))
channel_id = data.get('id')
if channel_type is ChannelType.group:
channel = self._get_private_channel(channel_id)
old_channel = copy.copy(channel)
channel._update_group(**data)
self.dispatch('channel_update', old_channel, channel)
return
server = self._get_server(data.get('guild_id'))
if server is not None:
channel_id = data.get('id')
channel = server.get_channel(channel_id)
if channel is not None:
old_channel = copy.copy(channel)

Loading…
Cancel
Save