diff --git a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py index f699e19..f7b051a 100644 --- a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py +++ b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py @@ -118,6 +118,17 @@ class Commands(): return command + def what_command_type(self, trigger): + full_trigger_str = trigger.args[1] + if full_trigger_str.startswith(tuple(self.config.prefix_list)): + return "command" + elif full_trigger_str.startswith(self.bot.nick): + return "nickname_command" + elif "intent" in trigger.tags and trigger.tags["intent"] == "ACTION": + return "action_command" + else: + return "rule" + def is_real_command(self, trigger_dict): if trigger_dict["trigger_type"] == "command": @@ -132,6 +143,22 @@ class Commands(): else: return False + def is_rulematch(function, command_type): + """Determine if function could be called with a rule match""" + rulematch = False + if command_type == "command": + if hasattr(function, 'commands'): + command_aliases = function.commands + elif command_type == "nickname_command": + if hasattr(function, 'nickname_commands'): + command_aliases = function.nickname_commands + elif command_type == "action_command": + if hasattr(function, 'action_commands'): + command_aliases = function.action_commands + if '(.*)' in command_aliases: + rulematch = True + return rulematch + def get_commands_split(self, trigger, splitkey): commands = [] diff --git a/sopel_SpiceBot_Core_Prerun/__init__.py b/sopel_SpiceBot_Core_Prerun/__init__.py index 2685b3e..a69cd26 100644 --- a/sopel_SpiceBot_Core_Prerun/__init__.py +++ b/sopel_SpiceBot_Core_Prerun/__init__.py @@ -3,7 +3,7 @@ import functools from sopel_SpiceBot_Core_1 import sb -def prerun(rulematch=False): +def prerun(): """This decorator is the hub of handling for all SpiceBot Commands""" def actual_decorator(function): @@ -11,28 +11,17 @@ def prerun(rulematch=False): @functools.wraps(function) def internal_prerun(bot, trigger, *args, **kwargs): - # trigger_type = [x for x in dir(function) if not x.startswith("__")][1] - if hasattr(function, 'commands'): - print("commands") - print(function.commands) - if hasattr(function, 'nickname_commands'): - print("nickname_commands") - print(function.nickname_commands) - if hasattr(function, 'action_commands'): - print("action_commands") - print(function.action_commands) + comrun = ComRun(function, trigger) # Get list of trigger command(s) by && split commands = sb.commands.get_commands_split(trigger, "&&") - comrun = ComRun(rulematch, trigger) - # Since there was more than one command, # we are going to redispatch commands # This will give sopel the appearance of recieving individual commands if len(commands) > 1: for trigger_dict in commands: - if rulematch: + if comrun.is_rulematch: if not sb.commands.is_real_command(trigger_dict): sb.commands.dispatch(trigger_dict) else: @@ -53,11 +42,10 @@ def prerun(rulematch=False): trigger_command = sb.commands.get_command_from_trigger(trigger) if trigger_command != commands[0]["trigger_command"]: trigger_dict = rebuild_pipes(commands) - sb.osd("pipes - dispatching %s %s" % (trigger_dict["trigger_command"], trigger_dict["trigger_str"]), trigger.sender) sb.commands.dispatch(trigger_dict) return - if rulematch and sb.commands.is_real_command(commands[0]): + if comrun.is_rulematch and sb.commands.is_real_command(commands[0]): return # This is where we rebuild trigger @@ -98,7 +86,14 @@ def rebuild_pipes(commands): class ComRun(): - def __init__(self, rulematch, trigger): - self.rulematch = rulematch - self.original_trigger = trigger - self.trigger_dict = {} + def __init__(self, function, trigger): + self.function = function + self.trigger = trigger + + @property + def command_type(self): + return sb.commands.what_command_type(self.trigger) + + @property + def is_rulematch(self): + return sb.commands.is_rulematch(self.function, self.command_type)