Source code for glitch.plugins.admin

"""Admin plugin"""
# Bot admin plugin. Kinda needed to do stuff
import importlib.util
import logging
import os.path

import yaml

from .. import commands, manage_plugin_config, plugin_map
from ... import config, messenger, plugins, save_config, scheduler

log = logging.getLogger(__name__)


@commands('die')
def die(event):
    messenger.send('quitAll')


@commands('reload plugins')
def reload_plugins(event):
    messenger.send('load all plugins')
    event.out_message = "Plugins reloading."
    event.send()


@commands('reload config')
def reload_config(event):
    with open('bot.yaml') as stream:
        global config
        config = yaml.load(stream)
        event.out_message = 'Config Reloaded'
        event.send()


@commands('add plugin', 'enable plugin')
[docs]def add_plugin(event): """Space delimited list on plugins to try to add. Determines if plugin exists. Add to and save new config Reload plugins.""" plugins = event.in_message.split(' ') reboot = False for plugin_name in plugins: #Copied from load_plugin spec = importlib.util.find_spec('glitch.plugins.' + plugin_name) if not spec: event.out_message = "Plugin not found: " + plugin_name log.error("Plugin %s not found in system paths.", plugin_name) try: plugin = importlib.import_module('glitch.plugins.' + plugin_name) plugin_map[plugin_name] = plugin reboot = False if hasattr(plugin, 'CONFIG') and hasattr(plugin, 'CONFIG_VERSION'): reboot = manage_plugin_config(plugin_name, plugin.CONFIG, plugin.CONFIG_VERSION) config['plugins'].append(plugin_name) save_config() event.out_message = "Plugin loaded: " + plugin_name log.info("Plugin %s loaded.", plugin_name) except Exception as e: event.out_message = "Error when loading plugin: " + plugin_name log.critical("Error when loading %s.", plugin_name) log.exception(e) if reboot: event.out_message = "!!!!ATTENTION!!!! There have been critical updates to config file and the bot will now shut down. Please review configuration before restarting." log.critical("!!!!ATTENTION!!!! There have been critical updates to the config file and the bot will now shut down. Please review configuration before restarting.") event.send() die(event)
@commands('show cron') def show_cron(event): event.out_message = str(scheduler.get_jobs()) event.send() @commands('list all plugins') def list_plugins(event): dir = os.path.dirname(plugins.__file__) event.out_message = "Currently not available." event.send() #for source in [f for f in glob.glob(eyercbot.exepath + '\\plugins') if '.py' not in f] #eyercbot.send('msg', server, user, str( # [f for f in glob.glob(eyercbot.exepath + '\\plugins//*') if # '.py' not in f])) @commands('add channel')
[docs]def add_channel(event): """Permanently joins a channel on the given server in a space delimited list.""" channels = event.in_message.split() if not channels: event.out_message = add_channel.__doc__ event.send() return for channel in channels: messenger.send('join channel', event.server_name, channel) config['connections']['irc'][event.server_name]['channels'].append(channel) save_config()
@commands('get config key') def get_config_key(event): if event.in_message in config: event.out_message = str(config[event.in_message]) else: event.out_message = 'I do not have a key by that name.' event.send() @commands('get plugin key') def get_plugin_key(event): if event.in_message in config['plugin_config']: event.out_message = str(config['plugin_config'][event.in_message]) else: event.out_message = 'I do not have a key by that name.' event.send()