2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-09-01 10:29:26 +00:00
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-12-02 09:18:24 +00:00
|
|
|
def _catch(self, name, func):
|
2018-12-02 09:16:17 +00:00
|
|
|
try:
|
2019-06-19 22:11:56 +00:00
|
|
|
return func()
|
2018-12-02 09:16:17 +00:00
|
|
|
except ModuleManager.ModuleNotFoundException:
|
2019-06-19 22:11:56 +00:00
|
|
|
raise utils.EventError("Module '%s' not found" % name)
|
|
|
|
except ModuleManager.ModuleNotLoadedException:
|
2018-12-02 09:16:17 +00:00
|
|
|
raise utils.EventError("Module '%s' isn't loaded" % name)
|
|
|
|
except ModuleManager.ModuleWarning as warning:
|
|
|
|
raise utils.EventError("Module '%s' not loaded: %s" % (
|
|
|
|
name, str(warning)))
|
|
|
|
except Exception as e:
|
|
|
|
raise utils.EventError("Failed to reload module '%s': %s" % (
|
|
|
|
name, str(e)))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.loadmodule", min_args=1)
|
2018-09-01 10:29:26 +00:00
|
|
|
def load(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Load a module
|
|
|
|
:usage: <module name>
|
|
|
|
:permission: load-module
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-01 10:29:26 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
if name in self.bot.modules.modules:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Module '%s' is already loaded" % name)
|
2019-06-19 22:11:56 +00:00
|
|
|
definition = self._catch(name,
|
|
|
|
lambda: self.bot.modules.find_module(name))
|
2018-12-02 09:16:17 +00:00
|
|
|
|
2019-05-27 13:57:16 +00:00
|
|
|
self._catch(name, lambda: self.bot.modules.load_module(self.bot, definition))
|
2018-09-01 10:29:26 +00:00
|
|
|
event["stdout"].write("Loaded '%s'" % name)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.unloadmodule", min_args=1)
|
2018-09-01 10:29:26 +00:00
|
|
|
def unload(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Unload a module
|
|
|
|
:usage: <module name>
|
|
|
|
:permission: unload-module
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-01 10:29:26 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
if not name in self.bot.modules.modules:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Module '%s' isn't loaded" % name)
|
2018-12-02 09:16:17 +00:00
|
|
|
|
2018-12-02 09:18:24 +00:00
|
|
|
self._catch(name, lambda: self.bot.modules.unload_module(name))
|
2018-09-01 10:29:26 +00:00
|
|
|
event["stdout"].write("Unloaded '%s'" % name)
|
|
|
|
|
2019-05-27 15:55:02 +00:00
|
|
|
def _reload(self, name):
|
|
|
|
self.bot.modules.unload_module(name)
|
2019-06-19 22:11:56 +00:00
|
|
|
definition = self._catch(name,
|
|
|
|
lambda: self.bot.modules.find_module(name))
|
2019-05-27 16:04:21 +00:00
|
|
|
self.bot.modules.load_module(self.bot, definition)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.reloadmodule", min_args=1)
|
2018-09-24 12:10:39 +00:00
|
|
|
def reload(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Reload a module
|
|
|
|
:usage: <module name>
|
|
|
|
:permission: reload-module
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-24 12:10:39 +00:00
|
|
|
name = event["args_split"][0].lower()
|
2018-12-02 09:16:17 +00:00
|
|
|
|
2018-12-02 09:19:03 +00:00
|
|
|
self._catch(name, lambda: self._reload(name))
|
2018-09-24 12:10:39 +00:00
|
|
|
event["stdout"].write("Reloaded '%s'" % name)
|
2018-09-23 10:01:24 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.reloadallmodules")
|
2018-09-23 10:01:24 +00:00
|
|
|
def reload_all(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Reload all modules
|
|
|
|
:permission: reload-all-modules
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-10-14 11:55:32 +00:00
|
|
|
result = self.bot.try_reload_modules()
|
|
|
|
if result.success:
|
|
|
|
event["stdout"].write(result.message)
|
2018-09-24 12:18:15 +00:00
|
|
|
else:
|
2019-10-14 11:55:32 +00:00
|
|
|
event["stderr"].write(result.message)
|
2018-09-23 10:01:24 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.enablemodule", min_args=1)
|
2018-09-13 12:35:05 +00:00
|
|
|
def enable(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Remove a module from the module blacklist
|
|
|
|
:usage: <module name>
|
|
|
|
:permission: enable-module
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-13 12:35:05 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
blacklist = self.bot.get_setting("module-blacklist", [])
|
|
|
|
if not name in blacklist:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Module '%s' isn't disabled" % name)
|
2018-09-13 12:35:05 +00:00
|
|
|
|
|
|
|
blacklist.remove(name)
|
2019-05-25 11:36:07 +00:00
|
|
|
self.bot.set_setting("module-blacklist", blacklist)
|
2018-09-13 12:35:05 +00:00
|
|
|
event["stdout"].write("Module '%s' has been enabled and can now "
|
|
|
|
"be loaded" % name)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.disablemodule", min_args=1)
|
2018-09-13 12:35:05 +00:00
|
|
|
def disable(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Add a module to the module blacklist
|
|
|
|
:usage: <module name>
|
|
|
|
:permission: disable-module
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-13 12:35:05 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
and_unloaded = ""
|
|
|
|
if name in self.bot.modules.modules:
|
|
|
|
self.bot.modules.unload_module(name)
|
|
|
|
and_unloaded = " and unloaded"
|
|
|
|
|
|
|
|
blacklist = self.bot.get_setting("module-blacklist", [])
|
|
|
|
if name in blacklist:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Module '%s' is already disabled" % name)
|
2018-09-13 12:35:05 +00:00
|
|
|
|
|
|
|
blacklist.append(name)
|
|
|
|
self.bot.set_setting("module-blacklist", blacklist)
|
|
|
|
event["stdout"].write("Module '%s' has been disabled%s" % (
|
|
|
|
name, and_unloaded))
|