Fix bug where the everyone role was not being properly resolved.

The permissions_for assumed that the everyone role would be the first
element of the permission overwrites but this is not guaranteed so we
have to guarantee it ourselves.
This commit is contained in:
Rapptz 2016-02-24 15:13:20 -05:00
parent 489363f5f8
commit 13f8b972e6

View File

@ -99,14 +99,26 @@ class Channel(Hashable):
self.changed_roles = []
self._permission_overwrites = []
for overridden in kwargs.get('permission_overwrites', []):
everyone_index = 0
everyone_id = self.server.default_role.id
for index, overridden in enumerate(kwargs.get('permission_overwrites', [])):
overridden_id = overridden['id']
self._permission_overwrites.append(Overwrites(**overridden))
if overridden.get('type') == 'member':
continue
if overridden_id == everyone_id:
# the @everyone role is not guaranteed to be the first one
# in the list of permission overwrites, however the permission
# resolution code kind of requires that it is the first one in
# the list since it is special. So we need the index so we can
# swap it to be the first one.
everyone_index = index
# this is pretty inefficient due to the deep nested loops unfortunately
role = utils.find(lambda r: r.id == overridden['id'], self.server.roles)
role = utils.find(lambda r: r.id == overridden_id, self.server.roles)
if role is None:
continue
@ -116,6 +128,11 @@ class Channel(Hashable):
override.permissions.handle_overwrite(allowed, denied)
self.changed_roles.append(override)
# do the swap
tmp = self._permission_overwrites
if tmp:
tmp[everyone_index], tmp[0] = tmp[0], tmp[everyone_index]
@property
def is_default(self):
"""bool : Indicates if this is the default channel for the :class:`Server` it belongs to."""