#!/usr/bin/env python3 """ Loads, reloads and unloads modules on the fly. """ import sys import loader import module @module.require_admin @module.commands("reload") @module.thread(False) def f_reload(bot, trigger): """Reloads a module, for use by admins only.""" if len(trigger.args) < 2: return boy.reply("Reload what?") name = trigger.args[1] if name == "*" or name.upper() == "ALL THE THINGS": bot.load_modules() return bot.msg("done") if name not in sys.modules: name = "modules." + name if name not in sys.modules: return bot.msg(f"Module '{name}' not loaded, try the 'load' command.") loader.unload_module(bot, name) loader.load_module(bot, name) bot.msg(f"Module '{name}' reloaded.") @module.require_admin @module.commands("load") @module.thread(False) def f_load(bot, trigger): """Loads a module, for use by admins only.""" if len(trigger.args) < 2: return bot.msg('Load what?') name = trigger.args[1] if name in sys.modules: return bot.msg('Module already loaded, use reload.') try: loader.load_module(bot, name) except ModuleNotFoundError: if name.startswith("modules."): return bot.msg(f"Module not found: '{name}'") name = "modules." + name try: loader.load_module(bot, name) except ModuleNotFoundError: return bot.msg(f"Module not found: '{name}'") bot.msg(f"Module '{name}' loaded.") @module.require_admin @module.commands("unload") @module.thread(False) def f_unload(bot, trigger): """Unloads a module, for use by admins only.""" if len(trigger.args) < 2: return bot.msg('Unload what?') name = trigger.args[1] if name not in sys.modules: name = "modules." + name if name not in sys.modules: return bot.msg(f"Module '{name}' not loaded, try the 'load' command.") loader.unload_module(bot, name) bot.msg(f"Module '{name}' unloaded.")