Browse Source

Add Role.server attribute.

This breaks events that expected a server parameter for the role events.
pull/237/head
Rapptz 9 years ago
parent
commit
5016f12079
  1. 3
      discord/client.py
  2. 10
      discord/role.py
  3. 2
      discord/server.py
  4. 7
      discord/state.py
  5. 7
      docs/api.rst

3
discord/client.py

@ -2447,8 +2447,7 @@ class Client:
yield from utils._verify_successful_response(r)
data = yield from r.json(encoding='utf-8')
everyone = server.id == data.get('id')
role = Role(everyone=everyone, **data)
role = Role(server=server, **data)
# we have to call edit because you can't pass a payload to the
# http request currently.

10
discord/role.py

@ -54,6 +54,8 @@ class Role(Hashable):
The name of the role.
permissions : :class:`Permissions`
Represents the role's permissions.
server : :class:`Server`
The server the role belongs to.
colour : :class:`Colour`
Represents the role colour. An alias exists under ``color``.
hoist : bool
@ -68,10 +70,10 @@ class Role(Hashable):
"""
__slots__ = ['id', 'name', 'permissions', 'color', 'colour', 'position',
'managed', 'mentionable', '_is_everyone', 'hoist' ]
'managed', 'mentionable', 'hoist', 'server' ]
def __init__(self, **kwargs):
self._is_everyone = kwargs.get('everyone', False)
self.server = kwargs.pop('server')
self._update(**kwargs)
def __str__(self):
@ -87,13 +89,11 @@ class Role(Hashable):
self.managed = kwargs.get('managed', False)
self.mentionable = kwargs.get('mentionable', False)
self.color = self.colour
if 'everyone' in kwargs:
self._is_everyone = kwargs['everyone']
@property
def is_everyone(self):
"""Checks if the role is the @everyone role."""
return self._is_everyone
return self.server.id == self.id
@property
def created_at(self):

2
discord/server.py

@ -155,7 +155,7 @@ class Server(Hashable):
self.icon = guild.get('icon')
self.unavailable = guild.get('unavailable', False)
self.id = guild['id']
self.roles = [Role(everyone=(self.id == r['id']), **r) for r in guild.get('roles', [])]
self.roles = [Role(server=self, **r) for r in guild.get('roles', [])]
for data in guild.get('members', []):
roles = [self.default_role]

7
discord/state.py

@ -476,10 +476,9 @@ class ConnectionState:
def parse_guild_role_create(self, data):
server = self._get_server(data.get('guild_id'))
role_data = data.get('role', {})
everyone = server.id == role_data.get('id')
role = Role(everyone=everyone, **role_data)
role = Role(server=server, **role_data)
server.roles.append(role)
self.dispatch('server_role_create', server, role)
self.dispatch('server_role_create', role)
def parse_guild_role_delete(self, data):
server = self._get_server(data.get('guild_id'))
@ -491,7 +490,7 @@ class ConnectionState:
except ValueError:
return
else:
self.dispatch('server_role_delete', server, role)
self.dispatch('server_role_delete', role)
def parse_guild_role_update(self, data):
server = self._get_server(data.get('guild_id'))

7
docs/api.rst

@ -264,12 +264,13 @@ to handle it, which defaults to print a traceback and ignore the exception.
: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)
.. function:: on_server_role_create(role)
on_server_role_delete(role)
Called when a :class:`Server` creates or deletes a new :class:`Role`.
:param server: The :class:`Server` that was created or deleted.
To get the server it belongs to, use :attr:`Role.server`.
:param role: The :class:`Role` that was created or deleted.
.. function:: on_server_role_update(before, after)

Loading…
Cancel
Save