diff --git a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py index 18aa037..785e3a2 100644 --- a/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py +++ b/sopel_SpiceBot_Core_1/SBCore/commands/__init__.py @@ -1,4 +1,6 @@ +from operator import itemgetter +from difflib import SequenceMatcher from sopel.trigger import PreTrigger @@ -140,6 +142,22 @@ class Commands(): else: return "rule" + def search_similar_commands(self, searchitem): + + sim_listitems, sim_num = [], [] + command_types = ["command", "nickname_command", "action_command"] + searchlists = [self.valid_sopel_commands, self.valid_sopel_nickname_commands, self.valid_sopel_action_commands] + for command_type, searchlist in zip(command_types, searchlists): + for listitem in searchlist: + similarlevel = SequenceMatcher(None, searchitem.lower(), listitem.lower()).ratio() + if similarlevel >= .75: + # command_type + sim_listitems.append(listitem) + sim_num.append(similarlevel) + + if len(sim_listitems) and len(sim_num): + sim_num, sim_listitems = (list(x) for x in zip(*sorted(zip(sim_num, sim_listitems), key=itemgetter(0)))) + def is_real_command(self, trigger_dict): if trigger_dict["trigger_type"] == "command": diff --git a/sopel_SpiceBot_Core_Prerun/__init__.py b/sopel_SpiceBot_Core_Prerun/__init__.py index 28f3278..3be082b 100644 --- a/sopel_SpiceBot_Core_Prerun/__init__.py +++ b/sopel_SpiceBot_Core_Prerun/__init__.py @@ -48,7 +48,11 @@ def prerun(): # The command is not valid if comrun.is_catchall and not comrun.is_real_command: - bot.say("Invalid command: %s" % comrun.command["trigger_command"]) + valid_command_results = sb.commands.search_similar_commands(comrun.command["trigger_command"]) + if not len(valid_command_results): + comrun.osd("%s does not appear to be a valid command." % comrun.command["trigger_command"]) + else: + comrun.osd("%s does not appear to be a valid command. Possible Matches: %s" % (comrun.command["trigger_command"], valid_command_results)) return # At this point, we update the re.match for trigger