Add a way to unset a setting with !config

This commit is contained in:
jesopo 2019-04-07 17:36:29 +01:00
parent d4c7c41c79
commit 444f37c3da

View file

@ -7,6 +7,8 @@ CHANNELSETADD_HELP = ("Add to a specified channel setting for the "
class ConfigInvalidValue(Exception): class ConfigInvalidValue(Exception):
pass pass
class ConfigSettingInexistent(Exception):
pass
class ConfigResults(enum.Enum): class ConfigResults(enum.Enum):
Changed = 1 Changed = 1
@ -30,6 +32,9 @@ class ConfigChannelTarget(object):
channel_id = self._get_id() channel_id = self._get_id()
return self._bot.database.channel_settings.get(channel_id, setting, return self._bot.database.channel_settings.get(channel_id, setting,
default) default)
def del_setting(self, setting):
channel_id = self._get_id()
self._bot.database.channel_settings.delete(channel_id, setting)
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _set(self, category, event, target, array, arg_index=0): def _set(self, category, event, target, array, arg_index=0):
@ -228,13 +233,20 @@ class Module(ModuleManager.BaseModule):
raise ConfigInvalidValue() raise ConfigInvalidValue()
else: else:
existing_value = target.get_setting(setting, None) existing_value = target.get_setting(setting, None)
if not existing_value == None:
if setting.startswith("-"):
setting = setting[1:]
target.del_setting(setting)
else:
return ConfigResult(ConfigResults.Retrieved, existing_value) return ConfigResult(ConfigResults.Retrieved, existing_value)
else:
raise ConfigSettingInexistent()
@utils.hook("received.command.config", min_args=1) @utils.hook("received.command.config", min_args=1)
def config(self, event): def config(self, event):
""" """
:help: Change config options :help: Change config options
:usage: <context>[:name] [setting [value]] :usage: <context>[:name] [-][setting [value]]
:permission: config :permission: config
""" """
@ -284,6 +296,9 @@ class Module(ModuleManager.BaseModule):
result = self._config(export_settings, target, setting, value) result = self._config(export_settings, target, setting, value)
except ConfigInvalidValue: except ConfigInvalidValue:
raise utils.EventError("Invalid value") raise utils.EventError("Invalid value")
except ConfigSettingInexistent:
raise utils.EventError("Setting not set")
if result.result == ConfigResults.Changed: if result.result == ConfigResults.Changed:
event["stdout"].write("Config changed") event["stdout"].write("Config changed")
elif result.result == ConfigResults.Retrieved: elif result.result == ConfigResults.Retrieved: