Browse Source

fix: handle loading of user permission levels

pull/136/head
Andrei 6 years ago
parent
commit
839682291b
No known key found for this signature in database GPG Key ID: 4D2A02C7D500E9D9
  1. 11
      disco/bot/bot.py
  2. 16
      disco/types/base.py
  3. 20
      disco/util/enum.py

11
disco/bot/bot.py

@ -16,6 +16,7 @@ from disco.util.config import Config
from disco.util.logging import LoggingClass from disco.util.logging import LoggingClass
from disco.util.serializer import Serializer from disco.util.serializer import Serializer
from disco.util.threadlocal import ThreadLocal from disco.util.threadlocal import ThreadLocal
from disco.util.enum import get_enum_value_by_name
class BotConfig(Config): class BotConfig(Config):
@ -185,9 +186,13 @@ class Bot(LoggingClass):
for plugin_mod in self.config.plugins: for plugin_mod in self.config.plugins:
self.add_plugin_module(plugin_mod) self.add_plugin_module(plugin_mod)
# Convert level mapping # Convert our configured mapping of entities to levels into something
for k, v in list(six.iteritems(self.config.levels)): # we can actually use. This ensures IDs are converted properly, and maps
self.config.levels[int(k) if k.isdigit() else k] = CommandLevels.get(v) # any level names (e.g. `role_id: admin`) map to their numerical values.
for entity_id, level in six.iteritems(self.config.levels):
entity_id = int(entity_id) if str(entity_id).isdigit() else entity_id
level = int(level) if str(level).isdigit() else get_enum_value_by_name(CommandLevels, level)
self.config.levels[entity_id] = level
@classmethod @classmethod
def from_cli(cls, *plugins): def from_cli(cls, *plugins):

16
disco/types/base.py

@ -7,6 +7,7 @@ from datetime import datetime as real_datetime
from disco.util.chains import Chainable from disco.util.chains import Chainable
from disco.util.hashmap import HashMap from disco.util.hashmap import HashMap
from disco.util.enum import get_enum_members
DATETIME_FORMATS = [ DATETIME_FORMATS = [
'%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S.%f',
@ -191,23 +192,12 @@ def snowflake(data):
return int(data) if data else None return int(data) if data else None
def _enum_attrs(enum):
for k, v in six.iteritems(enum.__dict__):
if not isinstance(k, six.string_types):
continue
if k.startswith('_') or not k.isupper():
continue
yield k, v
def enum(typ): def enum(typ):
def _f(data): def _f(data):
if data is None: if data is None:
return None return None
for k, v in _enum_attrs(typ): for k, v in get_enum_members(typ):
if isinstance(data, six.string_types) and k == data.upper(): if isinstance(data, six.string_types) and k == data.upper():
return v return v
elif k == data or v == data: elif k == data or v == data:
@ -396,7 +386,7 @@ class Model(six.with_metaclass(ModelMeta, Chainable)):
if ignore and name in ignore: if ignore and name in ignore:
continue continue
if getattr(self, name) == UNSET: if getattr(self, name) is UNSET:
continue continue
obj[name] = field.serialize(getattr(self, name), field) obj[name] = field.serialize(getattr(self, name), field)
return obj return obj

20
disco/util/enum.py

@ -0,0 +1,20 @@
import six
def get_enum_members(enum):
for k, v in six.iteritems(enum.__dict__):
if not isinstance(k, six.string_types):
continue
if k.startswith('_') or not k.isupper():
continue
yield k, v
def get_enum_value_by_name(enum, name):
name = name.lower()
for k, v in get_enum_members(enum):
if k.lower() == name:
return v
Loading…
Cancel
Save