Skip to content

Instantly share code, notes, and snippets.

@Zwork101
Created October 31, 2018 10:28
Show Gist options
  • Save Zwork101/1f9ebe84735c910f4f9b26d358be2151 to your computer and use it in GitHub Desktop.
Save Zwork101/1f9ebe84735c910f4f9b26d358be2151 to your computer and use it in GitHub Desktop.
Discord Perms
from enum import Enum
from typing import Union, List
PossiblePermissionType = Union[Enum, str, int]
class Permissions(Enum):
CREATE_INSTANT_INVITE = 0x00000001
KICK_MEMBERS = 0x00000002
BAN_MEMBERS = 0x00000004
ADMINISTRATOR = 0x00000008
MANAGE_CHANNELS = 0x00000010
MANAGE_GUILD = 0x00000020
ADD_REACTIONS = 0x00000040
VIEW_AUDIT_LOG = 0x00000080
VIEW_CHANNEL = 0x00000400
SEND_MESSAGES = 0x00000800
SEND_TTS_MESSAGES = 0x00001000
MANAGE_MESSAGES = 0x00002000
EMBED_LINKS = 0x00004000
ATTACH_FILES = 0x00008000
READ_MESSAGE_HISTORY = 0x00010000
MENTION_EVERYONE = 0x00020000
USE_EXTERNAL_EMOJIS = 0x00040000
CONNECT = 0x00100000
SPEAK = 0x00200000
MUTE_MEMBERS = 0x00400000
DEAFEN_MEMBERS = 0x00800000
MOVE_MEMBERS = 0x01000000
USE_VAD = 0x02000000
PRIORITY_SPEAKER = 0x00000100
CHANGE_NICKNAME = 0x04000000
MANAGE_NICKNAMES = 0x08000000
MANAGE_ROLES = 0x10000000
MANAGE_WEBHOOKS = 0x20000000
MANAGE_EMOJI = 0x40000000
def to_bitset(value: PossiblePermissionType) -> int:
if isinstance(value, Enum):
return value.value
elif isinstance(value, str):
for enum in Permissions:
if enum.name == value.upper().replace(' ', '_'):
return enum.value
else:
raise ValueError("Unable to find permission: '{0}'".format(value))
elif isinstance(value, int):
return value
raise ValueError("Invalid type for permission bitset. Must be shitcord.utils.permissions.Permissions, str, or int.")
class PermissionBitSet(int):
EMPTY = 0
def __contains__(self, item: PossiblePermissionType):
if self & to_bitset(item) == to_bitset(item):
return True
return False
def has(self, permissions: Union[List[PossiblePermissionType], PossiblePermissionType]) -> bool:
if not isinstance(permissions, list):
permissions = [permissions]
for permission in permissions:
if permission not in self:
break
else:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment