Add !enablemodule and !disablemodule to modules.py

This commit is contained in:
jesopo 2018-09-13 13:35:05 +01:00
parent b4397071ed
commit 1eed727bc5
3 changed files with 40 additions and 6 deletions

View file

@ -78,10 +78,10 @@ class ModuleManager(object):
else:
self.bot.log.error("Module '%s' not loaded", [name])
def load_modules(self, whitelist=None):
def load_modules(self, whitelist=[], blacklist=[]):
for path in self.list_modules():
name = self._module_name(path)
if whitelist == None or name in whitelist:
if name in whitelist or (not whitelist and not name in blacklist):
self.load_module(name)
def unload_module(self, name):

View file

@ -13,6 +13,13 @@ class Module(object):
min_args=1, permission="reload-module", help="Reload a module",
usage="<module-name>")
events.on("received.command.enablemodule").hook(self.enable,
min_args=1, permission="enable-module", help="Enable a module",
usage="<module-name>")
events.on("received.command.disablemodule").hook(self.disable,
min_args=1, permission="disable-module", help="Disable a module",
usage="<module-name>")
def load(self, event):
name = event["args_split"][0].lower()
if name in self.bot.modules.modules:
@ -37,3 +44,31 @@ class Module(object):
self.bot.modules.unload_module(name)
self.bot.modules.load_module(name)
event["stdout"].write("Reloaded '%s'" % name)
def enable(self, event):
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)
def disable(self, event):
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))

View file

@ -25,10 +25,9 @@ bot.database = database
bot.config = config.load_config()
bot.args = args
whitelist = bot.config.get("module_whitelist", None)
if not whitelist == None:
whitelist = whitelist.split(",")
bot.modules.load_modules(whitelist=whitelist)
whitelist = bot.get_setting("module-whitelist", [])
blacklist = bot.get_setting("module-blacklist", [])
bot.modules.load_modules(whitelist=whitelist, blacklist=blacklist)
server_details = database.servers.get_all()
servers = []