Add Channel.permissions_for and PrivateChannel.permissions_for.

These functions handle permission resolution for a specific member.

Aids with #18.
This commit is contained in:
Rapptz
2015-10-17 06:21:51 -04:00
parent 2813652995
commit 61f62c1468
2 changed files with 128 additions and 14 deletions

View File

@ -24,6 +24,16 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
def create_permission_masks(cls):
cls.NONE = cls(0)
cls.ALL = cls(0b00000011111100111111110000111111)
cls.ALL_CHANNEL = cls(0b00000011111100111111110000011001)
cls.GENERAL = cls(0b00000000000000000000000000111111)
cls.TEXT = cls(0b00000000000000111111110000000000)
cls.VOICE = cls(0b00000011111100000000000000000000)
return cls
@create_permission_masks
class Permissions(object):
"""Wraps up the Discord permission value.
@ -53,6 +63,21 @@ class Permissions(object):
else:
raise TypeError('Value to set for Permissions must be a bool.')
def handle_overwrite(self, allow, deny):
# 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.
self.value = ((self.value ^ deny) & ~deny) | allow
@property
def can_create_instant_invite(self):
"""Returns True if the user can create instant invites."""