Browse Source

Move permission overwrite construction to the Channel constructor.

pull/8/head
Rapptz 10 years ago
parent
commit
a9a030cdb2
  1. 29
      discord/channel.py
  2. 27
      discord/client.py

29
discord/channel.py

@ -23,6 +23,9 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from copy import deepcopy
from . import utils
class Channel(object):
"""Represents a Discord server channel.
@ -59,7 +62,31 @@ class Channel(object):
self.is_private = False
self.position = kwargs.get('position')
self.type = kwargs.get('type')
self.changed_roles = kwargs.get('permission_overwrites', [])
self.changed_roles = []
for overridden in kwargs.get('permission_overwrites', []):
# this is pretty inefficient due to the deep nested loops unfortunately
role = utils.find(lambda r: r.id == overridden['id'], self.server.roles)
if role is None:
continue
denied = overridden.get('deny', 0)
allowed = overridden.get('allow', 0)
override = deepcopy(role)
# Basically this is what's happening here.
# We have an original bit array, e.g. 1010
# Then we have another bit array that is 'denied', e.g. 1111
# And then we have the last one which is 'allowed', e.g. 0101
# We want original OP denied to end up resulting in
# whatever is in denied to be set to 0.
# So 1010 OP 1111 -> 0000
# Then we take this value and look at the allowed values.
# And whatever is allowed is set to 1.
# So 0000 OP2 0101 -> 0101
# The OP is (base ^ denied) & ~denied.
# The OP2 is base | allowed.
override.permissions.value = ((override.permissions.value ^ denied) & ~denied) | allowed
self.changed_roles.append(override)
class PrivateChannel(object):
"""Represents a Discord private channel.

27
discord/client.py

@ -163,33 +163,6 @@ class Client(object):
for member in server.members:
member.server = server
for channel in guild['channels']:
changed_roles = []
permission_overwrites = channel['permission_overwrites']
for overridden in permission_overwrites:
# this is pretty inefficient due to the deep nested loops unfortunately
role = utils.find(lambda r: r.id == overridden['id'], guild['roles'])
if role is None:
continue
denied = overridden.get('deny', 0)
allowed = overridden.get('allow', 0)
override = copy.deepcopy(role)
# Basically this is what's happening here.
# We have an original bit array, e.g. 1010
# Then we have another bit array that is 'denied', e.g. 1111
# And then we have the last one which is 'allowed', e.g. 0101
# We want original OP denied to end up resulting in whatever is in denied to be set to 0.
# So 1010 OP 1111 -> 0000
# Then we take this value and look at the allowed values. And whatever is allowed is set to 1.
# So 0000 OP2 0101 -> 0101
# The OP is (base ^ denied) & ~denied.
# The OP2 is base | allowed.
override.permissions.value = ((override.permissions.value ^ denied) & ~denied) | allowed
changed_roles.append(override)
channel['permission_overwrites'] = changed_roles
channels = [Channel(server=server, **channel) for channel in guild['channels']]
server.channels = channels
self.servers.append(server)

Loading…
Cancel
Save