Browse Source

Handle GUILD_UPDATE

pull/57/head
Rapptz 9 years ago
parent
commit
80b32e769e
  1. 7
      discord/client.py
  2. 15
      discord/server.py
  3. 7
      discord/state.py
  4. 12
      docs/api.rst

7
discord/client.py

@ -231,13 +231,12 @@ class Client:
if event in ('READY', 'MESSAGE_CREATE', 'MESSAGE_DELETE',
'MESSAGE_UPDATE', 'PRESENCE_UPDATE', 'USER_UPDATE',
'CHANNEL_DELETE', 'CHANNEL_UPDATE', 'CHANNEL_CREATE',
'GUILD_MEMBER_ADD', 'GUILD_MEMBER_REMOVE',
'GUILD_MEMBER_ADD', 'GUILD_MEMBER_REMOVE', 'GUILD_UPDATE'
'GUILD_MEMBER_UPDATE', 'GUILD_CREATE', 'GUILD_DELETE',
'GUILD_ROLE_CREATE', 'GUILD_ROLE_DELETE', 'TYPING_START',
'GUILD_ROLE_UPDATE', 'VOICE_STATE_UPDATE'):
parser = 'parse_' + event.lower()
if hasattr(self.connection, parser):
getattr(self.connection, parser)(data)
getattr(self.connection, parser)(data)
else:
log.info("Unhandled event {}".format(event))
@ -1739,7 +1738,7 @@ class Client:
deny = discord.Permissions.none()
allow.can_mention_everyone = True
deny.can_manage_messages = True
client.set_channel_permissions(message.channel, message.author, allow, deny)
yield from client.set_channel_permissions(message.channel, message.author, allow, deny)
Parameters
-----------

15
discord/server.py

@ -65,6 +65,9 @@ class Server:
"""
def __init__(self, **kwargs):
self.channels = []
self.owner = None
self.members = []
self._from_data(kwargs)
def _update_voice_state(self, data):
@ -91,10 +94,9 @@ class Server:
self.roles = [Role(everyone=(self.id == r['id']), **r) for r in guild['roles']]
default_role = self.get_default_role()
self.members = []
self.owner = guild['owner_id']
owner_id = guild['owner_id']
for data in guild['members']:
for data in guild.get('members', []):
roles = [default_role]
for role_id in data['roles']:
role = utils.find(lambda r: r.id == role_id, self.roles)
@ -105,7 +107,7 @@ class Server:
member = Member(**data)
member.server = self
if member.id == self.owner:
if member.id == owner_id:
self.owner = member
self.members.append(member)
@ -121,7 +123,10 @@ class Server:
pass
member.game_id = presence['game_id']
self.channels = [Channel(server=self, **c) for c in guild['channels']]
if 'channels' in guild:
channels = guild['channels']
self.channels = [Channel(server=self, **c) for c in channels]
afk_id = guild.get('afk_channel_id')
self.afk_channel = utils.find(lambda c: c.id == afk_id, self.channels)

7
discord/state.py

@ -222,6 +222,13 @@ class ConnectionState:
self._add_server(data)
self.dispatch('server_join', self.servers[-1])
def parse_guild_update(self, data):
server = self._get_server(data.get('id'))
if server is not None:
old_server = copy.copy(server)
server._from_data(data)
self.dispatch('server_update', old_server, server)
def parse_guild_delete(self, data):
server = self._get_server(data.get('id'))
if data.get('unavailable', False) and server is not None:

12
docs/api.rst

@ -243,6 +243,18 @@ to handle it, which defaults to print a traceback and ignore the exception.
:param server: The :class:`Server` that got removed.
.. function:: on_server_update(before, after)
Called when a :class:`Server` updates, for example:
- Changed name
- Changed AFK channel
- Changed AFK timeout
- etc
:param before: The :class:`Server` prior to being updated.
:param after: The :class:`Server` after being updated.
.. function:: on_server_role_create(server, role)
on_server_role_delete(server, role)

Loading…
Cancel
Save