diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index 155109b..ff56d4b 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -21,6 +21,7 @@ from .Config import config as botconfig from .Database import db as botdb from .Read import read as botread from .Channels import channels as botchannels +from .Users import users as botusers class SpiceBot_Commands_MainSection(StaticSection): @@ -91,21 +92,32 @@ class BotCommands(): return realcom + def chan_or_nick(self, target): + if botchannels.check_channel_bot(target, True): + return "channel" + elif botdb.check_nick_id(target): + return "nick" + else: + raise Exception(str(target.lower()) + " appears to not be a valid user/channel") + def get_commands_disabled(self, target, disabletype="fully"): disabletype = str(disabletype) + target_type = self.chan_or_nick(target) if disabletype not in list(self.dict['disabled'].keys()): self.dict['disabled'][disabletype] = { - "users": {}, - "channels": {} + "nick": {}, + "channel": {} } - if botdb.check_nick_id(target): - if target not in list(self.dict['disabled'][disabletype]["users"].keys()): - self.dict['disabled'][disabletype]["users"][target] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} - return self.dict['disabled'][disabletype]["users"][target] - elif botchannels.check_channel_bot(target, True): - if target not in list(self.dict['disabled'][disabletype]["channels"].keys()): - self.dict['disabled'][disabletype]["channels"][target] = botdb.get_channel_value(target, 'commands_' + disabletype + 'disabled') or {} - return self.dict['disabled'][disabletype]["channels"][target] + if target_type == "nick": + nick_id = botusers.ID(target) + if nick_id not in list(self.dict['disabled'][disabletype]["nick"].keys()): + self.dict['disabled'][disabletype]["nick"][nick_id] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} + return self.dict['disabled'][disabletype]["nick"][nick_id] + elif target_type == "channel": + target = str(target).lower() + if target not in list(self.dict['disabled'][disabletype]["channel"].keys()): + self.dict['disabled'][disabletype]["channel"][target] = botdb.get_channel_value(target, 'commands_' + disabletype + 'disabled') or {} + return self.dict['disabled'][disabletype]["channel"][target] else: raise Exception(str(target.lower()) + " appears to not be a valid user/channel") @@ -116,42 +128,50 @@ class BotCommands(): return False def set_command_disabled(self, command, target, timestamp, reason, bywhom, disabletype="fully"): + disabletype = str(disabletype) + target_type = self.chan_or_nick(target) if disabletype not in list(self.dict['disabled'].keys()): self.dict['disabled'][disabletype] = { - "users": {}, - "channels": {} + "nick": {}, + "channel": {} } - if botdb.check_nick_id(target): - if target not in list(self.dict['disabled'][disabletype]["users"].keys()): - self.dict['disabled'][disabletype]["users"][target] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} - self.dict['disabled'][disabletype]["users"][target][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} - botdb.set_nick_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["users"][target]) - elif botchannels.check_channel_bot(target, True): - if target not in list(self.dict['disabled'][disabletype]["channels"].keys()): - self.dict['disabled'][disabletype]["channels"][target] = botdb.get_channel_value(target, 'commands_' + disabletype + 'disabled') or {} - self.dict['disabled'][disabletype]["channels"][target][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} - botdb.set_channel_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["channels"][target]) + if target_type == "nick": + nick_id = botusers.ID(target) + if nick_id not in list(self.dict['disabled'][disabletype]["nick"].keys()): + self.dict['disabled'][disabletype]["nick"][nick_id] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} + self.dict['disabled'][disabletype]["nick"][nick_id][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} + botdb.set_nick_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["nick"][target]) + elif target_type == "channel": + target = str(target).lower() + if target not in list(self.dict['disabled'][disabletype]["channel"].keys()): + self.dict['disabled'][disabletype]["channel"][target] = botdb.get_channel_value(target, 'commands_' + disabletype + 'disabled') or {} + self.dict['disabled'][disabletype]["channel"][target][command] = {"reason": reason, "timestamp": timestamp, "disabledby": bywhom} + botdb.set_channel_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["channel"][target]) else: raise Exception(str(target.lower()) + " appears to not be a valid user/channel") def unset_command_disabled(self, command, target, disabletype="fully"): + disabletype = str(disabletype) + target_type = self.chan_or_nick(target) if disabletype not in list(self.dict['disabled'].keys()): self.dict['disabled'][disabletype] = { - "users": {}, - "channels": {} + "nick": {}, + "channel": {} } - if botdb.check_nick_id(target): - if target not in list(self.dict['disabled'][disabletype]["users"].keys()): - self.dict['disabled'][disabletype]["users"][target] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} - if command in list(self.dict['disabled'][disabletype]["users"].keys()): - del self.dict['disabled'][disabletype]["users"][target][command] - botdb.set_nick_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["users"][target]) - elif botchannels.check_channel_bot(target, True): - if target not in list(self.dict['disabled'][disabletype]["channels"].keys()): - self.dict['disabled'][disabletype]["users"][target] = botdb.get_channel_value(target, 'commands_' + disabletype + 'disabled') or {} - if command in list(self.dict['disabled'][disabletype]["channels"][target]): - del self.dict['disabled'][disabletype]["channels"][target][command] - botdb.set_channel_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["channels"][target]) + if target_type == "nick": + nick_id = botusers.ID(target) + if nick_id not in list(self.dict['disabled'][disabletype]["nick"].keys()): + self.dict['disabled'][disabletype]["nick"][nick_id] = botdb.get_nick_value(target, 'commands_' + disabletype + 'disabled') or {} + if command in list(self.dict['disabled'][disabletype]["nick"].keys()): + del self.dict['disabled'][disabletype]["nick"][nick_id][command] + botdb.set_nick_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["nick"][target]) + elif target_type == "channel": + target = str(target).lower() + if target not in list(self.dict['disabled'][disabletype]["channel"].keys()): + self.dict['disabled'][disabletype]["nick"][target] = botdb.get_channel_value(target, 'commands_' + disabletype + 'disabled') or {} + if command in list(self.dict['disabled'][disabletype]["channel"][target]): + del self.dict['disabled'][disabletype]["channel"][target][command] + botdb.set_channel_value(target, 'commands_' + disabletype + 'disabled', self.dict['disabled'][disabletype]["channel"][target]) else: raise Exception(str(target.lower()) + " appears to not be a valid user/channel") diff --git a/sopel_modules/SpiceBot/Prerun.py b/sopel_modules/SpiceBot/Prerun.py index 54b81e1..7ce64f3 100644 --- a/sopel_modules/SpiceBot/Prerun.py +++ b/sopel_modules/SpiceBot/Prerun.py @@ -247,7 +247,7 @@ def trigger_runstatus_query(bot, trigger): # don't run commands that are disabled in channels if not trigger.is_privmsg: - channel_disabled_list = botcommands.get_commands_disabled(str(trigger.sender), "fully") + channel_disabled_list = botcommands.get_commands_disabled(str(trigger.sender)) if trigger.sb["realcomref"] in list(channel_disabled_list.keys()): reason = channel_disabled_list[trigger.sb["realcomref"]]["reason"] timestamp = channel_disabled_list[trigger.sb["realcomref"]]["timestamp"] @@ -256,7 +256,7 @@ def trigger_runstatus_query(bot, trigger): return trigger_cant_run(bot, trigger, message) # don't run commands that are disabled for specific users - nick_disabled_list = botcommands.get_commands_disabled(str(trigger.nick), "fully") + nick_disabled_list = botcommands.get_commands_disabled(str(trigger.nick)) if trigger.sb["realcomref"] in list(nick_disabled_list.keys()): bywhom = nick_disabled_list[trigger.sb["realcomref"]]["disabledby"] if botusers.ID(bywhom) != botusers.ID(trigger.nick): @@ -265,7 +265,7 @@ def trigger_runstatus_query(bot, trigger): message = "The " + str(trigger.sb["comtext"]) + " command was disabled by " + bywhom + " for " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason) return trigger_cant_run(bot, trigger, message) else: - botcommands.unset_command_disabled(trigger.sb["realcomref"], trigger.nick, "fully") + botcommands.unset_command_disabled(trigger.sb["realcomref"], trigger.nick) botmessagelog.messagelog_error(trigger.sb["log_id"], trigger.sb["comtext"] + " is now enabled for " + str(trigger.nick)) return True