def __init__(self, client, config=None): self.client = client self.config = config or BotConfig() # Shard manager self.shards = None # The context carries information about events in a threadlocal storage self.ctx = ThreadLocal() # The storage object acts as a dynamic contextual aware store self.storage = None if self.config.storage_enabled: self.storage = Storage(self.ctx, self.config.from_prefix('storage')) # If the manhole is enabled, add this bot as a local if self.client.config.manhole_enable: self.client.manhole_locals['bot'] = self if self.config.http_enabled: from flask import Flask self.log.info('Starting HTTP server bound to %s:%s', self.config.http_host, self.config.http_port) self.http = Flask('disco') self.http_server = WSGIServer( (self.config.http_host, self.config.http_port), self.http) self.http_server_greenlet = gevent.spawn( self.http_server.serve_forever) self.plugins = {} self.group_abbrev = {} # Only bind event listeners if we're going to parse commands if self.config.commands_enabled: self.client.events.on('MessageCreate', self.on_message_create) if self.config.commands_allow_edit: self.client.events.on('MessageUpdate', self.on_message_update) # If we have a level getter and its a string, try to load it if isinstance(self.config.commands_level_getter, six.string_types): mod, func = self.config.commands_level_getter.rsplit('.', 1) mod = importlib.import_module(mod) self.config.commands_level_getter = getattr(mod, func) # Stores the last message for every single channel self.last_message_cache = {} # Stores a giant regex matcher for all commands self.command_matches_re = None # Finally, load all the plugin modules that where passed with the config for plugin_mod in self.config.plugins: self.add_plugin_module(plugin_mod) # Convert level mapping for k, v in six.iteritems(self.config.levels): self.config.levels[k] = CommandLevels.get(v)
def __init__(self, client, config=None): self.client = client self.config = config or BotConfig() # Shard manager self.shards = None # The context carries information about events in a threadlocal storage self.ctx = ThreadLocal() # The storage object acts as a dynamic contextual aware store self.storage = None if self.config.storage_enabled: self.storage = Storage(self.ctx, self.config.from_prefix('storage')) # If the manhole is enabled, add this bot as a local if self.client.config.manhole_enable: self.client.manhole_locals['bot'] = self self.plugins = {} self.group_abbrev = {} # Only bind event listeners if we're going to parse commands if self.config.commands_enabled: self.client.events.on('MessageCreate', self.on_message_create) if self.config.commands_allow_edit: self.client.events.on('MessageUpdate', self.on_message_update) # Stores the last message for every single channel self.last_message_cache = {} # Stores a giant regex matcher for all commands self.command_matches_re = None # Finally, load all the plugin modules that where passed with the config for plugin_mod in self.config.plugins: self.add_plugin_module(plugin_mod) # Convert level mapping for k, v in six.iteritems(self.config.levels): self.config.levels[k] = CommandLevels.get(v)
def on_rank_set(self, event, user, rank): # for some reason you need all lower-case to get the enum _rank = CommandLevels.get(rank.lower()) if not _rank: return event.msg.reply('Invalid rank') if not user: return current_rank = self.bot.get_level(user) author_rank = self.bot.get_level(event.author) if current_rank and author_rank: if current_rank > author_rank: return event.msg.reply( 'Woah there! You can\'t target this person.') self.bot.config.levels[user.id] = _rank event.msg.reply(f"{user.username} has been assigned '{rank}'")