2018-09-26 17:27:17 +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-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.loadmodule", min_args=1,
|
|
|
|
permission="load-module", usage="<module-name>")
|
2018-09-01 10:29:26 +00:00
|
|
|
def load(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Load a module
|
|
|
|
"""
|
2018-09-01 10:29:26 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
if name in self.bot.modules.modules:
|
|
|
|
event["stderr"].write("Module '%s' is already loaded" % name)
|
|
|
|
return
|
|
|
|
self.bot.modules.load_module(name)
|
|
|
|
event["stdout"].write("Loaded '%s'" % name)
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.unloadmodule", min_args=1,
|
|
|
|
permission="unload-module", usage="<module-name>")
|
2018-09-01 10:29:26 +00:00
|
|
|
def unload(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Unload a module
|
|
|
|
"""
|
2018-09-01 10:29:26 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
if not name in self.bot.modules.modules:
|
|
|
|
event["stderr"].write("Module '%s' isn't loaded" % name)
|
|
|
|
return
|
|
|
|
self.bot.modules.unload_module(name)
|
|
|
|
event["stdout"].write("Unloaded '%s'" % name)
|
|
|
|
|
2018-09-24 12:10:39 +00:00
|
|
|
def _reload(self, name):
|
2018-09-01 10:29:26 +00:00
|
|
|
self.bot.modules.unload_module(name)
|
|
|
|
self.bot.modules.load_module(name)
|
2018-09-23 10:01:24 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.reloadmodule", min_args=1,
|
|
|
|
permission="reload-module", usage="<module-name>")
|
2018-09-24 12:10:39 +00:00
|
|
|
def reload(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Reload a module
|
|
|
|
"""
|
2018-09-24 12:10:39 +00:00
|
|
|
name = event["args_split"][0].lower()
|
|
|
|
try:
|
|
|
|
self._reload(name)
|
|
|
|
except ModuleManager.ModuleNotFoundException:
|
|
|
|
event["stderr"].write("Module '%s' isn't loaded" % name)
|
|
|
|
return
|
2018-09-24 15:15:44 +00:00
|
|
|
except ModuleManager.ModuleWarning as warning:
|
|
|
|
event["stderr"].write("Module '%s' not loaded: %s" % (
|
2018-09-25 10:02:31 +00:00
|
|
|
name, str(warning)))
|
2018-09-24 15:15:44 +00:00
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
event["stderr"].write("Failed to reload module '%s': %s" % (
|
|
|
|
name, str(e)))
|
|
|
|
return
|
2018-09-24 12:10:39 +00:00
|
|
|
event["stdout"].write("Reloaded '%s'" % name)
|
2018-09-23 10:01:24 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.reloadallmodules", permission="reload-module")
|
2018-09-23 10:01:24 +00:00
|
|
|
def reload_all(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Reload all modules
|
|
|
|
"""
|
2018-09-24 12:10:39 +00:00
|
|
|
reloaded = []
|
|
|
|
failed = []
|
2018-09-24 12:18:15 +00:00
|
|
|
for name in list(self.bot.modules.modules.keys()):
|
2018-09-24 12:10:39 +00:00
|
|
|
try:
|
|
|
|
self._reload(name)
|
2018-09-24 12:18:15 +00:00
|
|
|
except ModuleWarning:
|
|
|
|
continue
|
|
|
|
except:
|
2018-09-24 12:10:39 +00:00
|
|
|
failed.append(name)
|
2018-09-24 12:18:15 +00:00
|
|
|
continue
|
|
|
|
reloaded.append(name)
|
2018-09-23 10:01:24 +00:00
|
|
|
|
2018-09-24 12:18:15 +00:00
|
|
|
if reloaded and failed:
|
|
|
|
event["stdout"].write("Reloaded %d modules, %d failed" % (
|
|
|
|
len(reloaded), len(failed)))
|
|
|
|
elif failed:
|
|
|
|
event["stdout"].write("Failed to reload all modules")
|
|
|
|
else:
|
|
|
|
event["stdout"].write("Reloaded %d modules" % len(reloaded))
|
2018-09-23 10:01:24 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.enablemodule", min_args=1,
|
|
|
|
permission="enable-module", usage="<module-name>")
|
2018-09-13 12:35:05 +00:00
|
|
|
def enable(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Remove a module from the module blacklist
|
|
|
|
"""
|
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:
|
|
|
|
event["stderr"].write("Module '%s' isn't disabled" % name)
|
|
|
|
return
|
|
|
|
|
|
|
|
blacklist.remove(name)
|
|
|
|
event["stdout"].write("Module '%s' has been enabled and can now "
|
|
|
|
"be loaded" % name)
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.disablemodule", min_args=1,
|
|
|
|
permission="disable-module", usage="<module-name>")
|
2018-09-13 12:35:05 +00:00
|
|
|
def disable(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Add a module to the module blacklist
|
|
|
|
"""
|
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:
|
|
|
|
event["stderr"].write("Module '%s' is already disabled" % name)
|
|
|
|
return
|
|
|
|
|
|
|
|
blacklist.append(name)
|
|
|
|
self.bot.set_setting("module-blacklist", blacklist)
|
|
|
|
event["stdout"].write("Module '%s' has been disabled%s" % (
|
|
|
|
name, and_unloaded))
|