aboutsummaryrefslogtreecommitdiff
path: root/discord/channel.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-09-25 15:53:45 -0400
committerRapptz <[email protected]>2015-09-25 15:53:45 -0400
commita9a030cdb23326e13a8e17a1eff25fcfe37efeee (patch)
tree74767512b6e8ffd73977b089591e552d8cf5bce8 /discord/channel.py
parentPRESENCE_UPDATE now updates the user as well. (diff)
downloaddiscord.py-a9a030cdb23326e13a8e17a1eff25fcfe37efeee.tar.xz
discord.py-a9a030cdb23326e13a8e17a1eff25fcfe37efeee.zip
Move permission overwrite construction to the Channel constructor.
Diffstat (limited to 'discord/channel.py')
-rw-r--r--discord/channel.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/discord/channel.py b/discord/channel.py
index 697872e3..28227178 100644
--- a/discord/channel.py
+++ b/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.