From 3bf05b9fce4da966369db12b3e65ad81bfc540ba Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 31 May 2019 11:39:36 -0400 Subject: [PATCH] commands reverse integration --- sopel_modules/SpiceBot/Commands.py | 133 ++++++++++++++++++ .../NickQueryCommand.py | 76 ---------- .../SpiceBot_CommandsQuery/QueryCommand.py | 74 ---------- .../SpiceBot_CommandsQuery/__init__.py | 13 -- 4 files changed, 133 insertions(+), 163 deletions(-) delete mode 100644 sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py delete mode 100644 sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py delete mode 100644 sopel_modules/SpiceBot_CommandsQuery/__init__.py diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index 3bc8fdd..3204787 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -14,6 +14,9 @@ import spicemanip from .Logs import logs from .Config import config as botconfig from .Database import db as botdb +from .Events import events +from .Prerun import prerun +from .Tools import letters_in_string, similar_list class BotCommands(): @@ -231,3 +234,133 @@ class BotCommands(): commands = BotCommands() + + +@events.check_ready([events.BOT_COMMANDSQUERY]) +@prerun.prerun('nickname') +@sopel.module.nickname_commands('(.*)') +def query_detection_nick(bot, trigger): + + # command issued, check if valid + if not trigger.sb['com'] or not len(trigger.sb['com']): + return + + if not trigger.sb['com'][0] == "?": + return + trigger.sb['com'] = trigger.sb['com'][1:] + + if not letters_in_string(trigger.sb['com']): + return + + commands_list = dict() + for com in commands.dict['commands']['nickname'].keys(): + if com not in commands_list.keys(): + commands_list[com] = commands.dict['commands']['nickname'][com] + + if trigger.sb['com'].endswith("+"): + + trigger.sb['com'] = trigger.sb['com'][:-1] + if not trigger.sb['com'] or not len(trigger.sb['com']): + return + + if trigger.sb['com'].lower() not in list(commands_list.keys()): + dispmsg = ["Cannot find any alias " + bot.nick + " commands: No valid commands match " + str(trigger.sb['com']) + "."] + closestmatches = similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') + if len(closestmatches): + dispmsg.append("The following " + bot.nick + " commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".") + bot.osd(dispmsg, trigger.nick, 'notice') + return + + realcom = trigger.sb['com'] + if "aliasfor" in commands_list[trigger.sb['com']].keys(): + realcom = commands_list[trigger.sb['com']]["aliasfor"] + validcomlist = commands_list[realcom]["validcoms"] + bot.osd("The following " + bot.nick + " commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick, 'notice') + return + + if trigger.sb['com'].endswith("?"): + + trigger.sb['com'] = trigger.sb['com'][:-1] + if not trigger.sb['com'] or not len(trigger.sb['com']): + return + + closestmatches = similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') + if not len(closestmatches): + bot.osd("Cannot find any similar " + bot.nick + " commands for " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') + else: + bot.osd("The following " + bot.nick + " commands may match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".", trigger.nick, 'notice') + return + + commandlist = [] + for command in list(commands_list.keys()): + if command.lower().startswith(str(trigger.sb['com']).lower()): + commandlist.append(command) + + if not len(commandlist): + bot.osd("No " + bot.nick + " commands start with " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') + else: + bot.osd("The following " + bot.nick + " commands start with " + str(trigger.sb['com']) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick, 'notice') + + +@events.check_ready([events.BOT_COMMANDSQUERY]) +@prerun.prerun('query') +@sopel.module.rule('^\?(.*)') +def query_detection(bot, trigger): + + # command issued, check if valid + if not trigger.sb['com'] or not len(trigger.sb['com']): + return + + if not letters_in_string(trigger.sb['com']): + return + + commands_list = dict() + for commandstype in commands.dict['commands'].keys(): + if commandstype not in ['rule', 'nickname']: + for com in commands.dict['commands'][commandstype].keys(): + if com not in commands_list.keys(): + commands_list[com] = commands.dict['commands'][commandstype][com] + + if trigger.sb['com'][:-1] == "+": + + trigger.sb['com'] = trigger.sb['com'][:-1] + if not trigger.sb['com'] or not len(trigger.sb['com']): + return + + if trigger.sb['com'].lower() not in list(commands_list.keys()): + dispmsg = ["Cannot find any alias commands: No valid commands match " + str(trigger.sb['com']) + "."] + closestmatches = similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') + if len(closestmatches): + dispmsg.append("The following commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".") + bot.osd(dispmsg, trigger.nick, 'notice') + return + + realcom = trigger.sb['com'] + if "aliasfor" in commands_list[trigger.sb['com']].keys(): + realcom = commands_list[trigger.sb['com']]["aliasfor"] + validcomlist = commands_list[realcom]["validcoms"] + bot.osd("The following commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick, 'notice') + return + + if trigger.sb['com'][:-1] == "?": + + trigger.sb['com'] = trigger.sb['com'][:-1] + if not trigger.sb['com'] or not len(trigger.sb['com']): + return + + closestmatches = similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') + if not len(closestmatches): + bot.osd("Cannot find any similar commands for " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') + else: + bot.osd("The following commands may match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".", trigger.nick, 'notice') + return + + commandlist = [] + for command in list(commands_list.keys()): + if command.lower().startswith(str(trigger.sb['com']).lower()): + commandlist.append(command) + + if not len(commandlist): + bot.osd("No commands start with " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') + else: + bot.osd("The following commands start with " + str(trigger.sb['com']) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick, 'notice') diff --git a/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py b/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py deleted file mode 100644 index d4fef62..0000000 --- a/sopel_modules/SpiceBot_CommandsQuery/NickQueryCommand.py +++ /dev/null @@ -1,76 +0,0 @@ -# coding=utf-8 - -from __future__ import unicode_literals, absolute_import, division, print_function - -import sopel -import sopel.module - -import sopel_modules.SpiceBot as SpiceBot - -import spicemanip - - -@SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY]) -@SpiceBot.prerun.prerun('nickname') -@sopel.module.nickname_commands('(.*)') -def query_detection_nick(bot, trigger): - - # command issued, check if valid - if not trigger.sb['com'] or not len(trigger.sb['com']): - return - - if not trigger.sb['com'][0] == "?": - return - trigger.sb['com'] = trigger.sb['com'][1:] - - if not SpiceBot.letters_in_string(trigger.sb['com']): - return - - commands_list = dict() - for com in SpiceBot.commands.dict['commands']['nickname'].keys(): - if com not in commands_list.keys(): - commands_list[com] = SpiceBot.commands.dict['commands']['nickname'][com] - - if trigger.sb['com'].endswith("+"): - - trigger.sb['com'] = trigger.sb['com'][:-1] - if not trigger.sb['com'] or not len(trigger.sb['com']): - return - - if trigger.sb['com'].lower() not in list(commands_list.keys()): - dispmsg = ["Cannot find any alias " + bot.nick + " commands: No valid commands match " + str(trigger.sb['com']) + "."] - closestmatches = SpiceBot.similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') - if len(closestmatches): - dispmsg.append("The following " + bot.nick + " commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".") - bot.osd(dispmsg, trigger.nick, 'notice') - return - - realcom = trigger.sb['com'] - if "aliasfor" in commands_list[trigger.sb['com']].keys(): - realcom = commands_list[trigger.sb['com']]["aliasfor"] - validcomlist = commands_list[realcom]["validcoms"] - bot.osd("The following " + bot.nick + " commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick, 'notice') - return - - if trigger.sb['com'].endswith("?"): - - trigger.sb['com'] = trigger.sb['com'][:-1] - if not trigger.sb['com'] or not len(trigger.sb['com']): - return - - closestmatches = SpiceBot.similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') - if not len(closestmatches): - bot.osd("Cannot find any similar " + bot.nick + " commands for " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') - else: - bot.osd("The following " + bot.nick + " commands may match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".", trigger.nick, 'notice') - return - - commandlist = [] - for command in list(commands_list.keys()): - if command.lower().startswith(str(trigger.sb['com']).lower()): - commandlist.append(command) - - if not len(commandlist): - bot.osd("No " + bot.nick + " commands start with " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') - else: - bot.osd("The following " + bot.nick + " commands start with " + str(trigger.sb['com']) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick, 'notice') diff --git a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py deleted file mode 100644 index 277db87..0000000 --- a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py +++ /dev/null @@ -1,74 +0,0 @@ -# coding=utf-8 - -from __future__ import unicode_literals, absolute_import, division, print_function - -import sopel -import sopel.module - -import sopel_modules.SpiceBot as SpiceBot - -import spicemanip - - -@SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY]) -@SpiceBot.prerun.prerun('query') -@sopel.module.rule('^\?(.*)') -def query_detection(bot, trigger): - - # command issued, check if valid - if not trigger.sb['com'] or not len(trigger.sb['com']): - return - - if not SpiceBot.letters_in_string(trigger.sb['com']): - return - - commands_list = dict() - for commandstype in SpiceBot.commands.dict['commands'].keys(): - if commandstype not in ['rule', 'nickname']: - for com in SpiceBot.commands.dict['commands'][commandstype].keys(): - if com not in commands_list.keys(): - commands_list[com] = SpiceBot.commands.dict['commands'][commandstype][com] - - if trigger.sb['com'][:-1] == "+": - - trigger.sb['com'] = trigger.sb['com'][:-1] - if not trigger.sb['com'] or not len(trigger.sb['com']): - return - - if trigger.sb['com'].lower() not in list(commands_list.keys()): - dispmsg = ["Cannot find any alias commands: No valid commands match " + str(trigger.sb['com']) + "."] - closestmatches = SpiceBot.similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') - if len(closestmatches): - dispmsg.append("The following commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".") - bot.osd(dispmsg, trigger.nick, 'notice') - return - - realcom = trigger.sb['com'] - if "aliasfor" in commands_list[trigger.sb['com']].keys(): - realcom = commands_list[trigger.sb['com']]["aliasfor"] - validcomlist = commands_list[realcom]["validcoms"] - bot.osd("The following commands match " + str(trigger.sb['com']) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick, 'notice') - return - - if trigger.sb['com'][:-1] == "?": - - trigger.sb['com'] = trigger.sb['com'][:-1] - if not trigger.sb['com'] or not len(trigger.sb['com']): - return - - closestmatches = SpiceBot.similar_list(trigger.sb['com'], list(commands_list.keys()), 10, 'reverse') - if not len(closestmatches): - bot.osd("Cannot find any similar commands for " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') - else: - bot.osd("The following commands may match " + str(trigger.sb['com']) + ": " + spicemanip.main(closestmatches, 'andlist') + ".", trigger.nick, 'notice') - return - - commandlist = [] - for command in list(commands_list.keys()): - if command.lower().startswith(str(trigger.sb['com']).lower()): - commandlist.append(command) - - if not len(commandlist): - bot.osd("No commands start with " + str(trigger.sb['com']) + ".", trigger.nick, 'notice') - else: - bot.osd("The following commands start with " + str(trigger.sb['com']) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick, 'notice') diff --git a/sopel_modules/SpiceBot_CommandsQuery/__init__.py b/sopel_modules/SpiceBot_CommandsQuery/__init__.py deleted file mode 100644 index 3dffefa..0000000 --- a/sopel_modules/SpiceBot_CommandsQuery/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# coding=utf8 -"""Sopel Commands Query - -Sopel Commands Query responds to ? to display commands matching patterns -""" -from __future__ import unicode_literals, absolute_import, division, print_function - -from .QueryCommand import * -from .NickQueryCommand import * - -__author__ = 'Sam Zick' -__email__ = 'sam@deathbybandaid.net' -__version__ = '0.1.1'