diff --git a/sopel_modules/SpiceBot_Channels/Channels.py b/sopel_modules/SpiceBot_Channels/Channels.py index 103df07..5ce9452 100644 --- a/sopel_modules/SpiceBot_Channels/Channels.py +++ b/sopel_modules/SpiceBot_Channels/Channels.py @@ -42,7 +42,8 @@ def setup(bot): bot.config.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) - bot_channels_setup_check(bot) + if "SpiceBot_Channels" not in bot.memory: + bot.memory["SpiceBot_Channels"] = {"channels": {}, "InitialProcess": False, "ProcessLock": False} def shutdown(bot): @@ -53,7 +54,6 @@ def shutdown(bot): @sopel.module.event('1003') @sopel.module.rule('.*') def trigger_channel_list_initial(bot, trigger): - bot_logging(bot, 'SpiceBot_Events', trigger.args[1]) bot_events_recieved(bot, trigger.event) # Unkickable @@ -108,13 +108,7 @@ def trigger_channel_list_initial(bot, trigger): del bot.memory['SpiceBot_Channels']['channels']["*"] -def bot_channels_setup_check(bot): - if "SpiceBot_Channels" not in bot.memory: - bot.memory["SpiceBot_Channels"] = {"channels": {}, "InitialProcess": False, "ProcessLock": False} - - @sopel.module.event('2001') @sopel.module.rule('.*') def bot_events_setup(bot, trigger): - bot_logging(bot, 'SpiceBot_Events', trigger.args[1]) bot_events_recieved(bot, trigger.event) diff --git a/sopel_modules/SpiceBot_Channels/Channels_Command.py b/sopel_modules/SpiceBot_Channels/Channels_Command.py index f678866..0914447 100644 --- a/sopel_modules/SpiceBot_Channels/Channels_Command.py +++ b/sopel_modules/SpiceBot_Channels/Channels_Command.py @@ -6,6 +6,7 @@ import sopel.module import spicemanip +from sopel_modules.SpiceBot_Events.System import bot_events_check from sopel_modules.SpiceBot_SBTools import ( sopel_triggerargs, inlist, channel_privs, join_all_channels, channel_list_current, @@ -15,6 +16,9 @@ from sopel_modules.SpiceBot_SBTools import ( @sopel.module.nickname_commands('channels', 'channel') def nickname_comand_channels(bot, trigger): + while not bot_events_check(bot, '1004'): + pass + triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'nickname_command') if not len(triggerargs): diff --git a/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py b/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py index fc2ee60..8c90401 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py +++ b/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py @@ -6,12 +6,10 @@ import sopel import sopel.module import os -from difflib import SequenceMatcher -from operator import itemgetter -import threading - -from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, check_bot_events, bot_logging +from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, check_bot_events, startup_bot_event +from sopel_modules.SpiceBot_Events.System import bot_events_startup_register, bot_events_recieved, bot_events_trigger +from sopel_modules.SpiceBot_SBTools import bot_logging import spicemanip @@ -21,9 +19,13 @@ def configure(config): def setup(bot): - bot_logging(bot, 'SpiceBot_CommandsQuery', "Evaluating Core Commands List") + bot_logging(bot, 'SpiceBot_CommandsQuery', "Starting setup procedure") + bot_events_startup_register(bot, ['2002']) - threading.Thread(target=setup_thread, args=(bot,)).start() + startup_bot_event(bot, "SpiceBot_CommandsQuery") + + if 'SpiceBot_CommandsQuery' not in bot.memory: + bot.memory['SpiceBot_CommandsQuery'] = {"counts": {}, "commands": {}} def shutdown(bot): @@ -31,16 +33,10 @@ def shutdown(bot): del bot.memory["SpiceBot_CommandsQuery"] -def setup_thread(bot): - - if 'SpiceBot_CommandsQuery' not in bot.memory: - bot.memory['SpiceBot_CommandsQuery'] = dict() - - if 'counts' not in bot.memory['SpiceBot_CommandsQuery'].keys(): - bot.memory['SpiceBot_CommandsQuery']['counts'] = dict() - - if 'commands' not in bot.memory['SpiceBot_CommandsQuery'].keys(): - bot.memory['SpiceBot_CommandsQuery']['commands'] = dict() +@sopel.module.event('1003') +@sopel.module.rule('.*') +def trigger_command_list_initial(bot, trigger): + bot_events_recieved(bot, trigger.event) for comtype in ['module', 'nickname', 'rule']: if comtype not in bot.memory['SpiceBot_CommandsQuery']['counts'].keys(): @@ -159,129 +155,25 @@ def setup_thread(bot): for comtype in ['module', 'nickname', 'rule']: bot_logging(bot, 'SpiceBot_CommandsQuery', "Found " + str(len(bot.memory['SpiceBot_CommandsQuery']['commands'][comtype].keys())) + " " + comtype + " commands.") - while not check_bot_events(bot, ["startup_complete"]): - pass + set_bot_event(bot, "SpiceBot_CommandsQuery") + bot_events_trigger(bot, 2002, "SpiceBot_CommandsQuery") + + +@sopel.module.event('1004') +@sopel.module.rule('.*') +def bot_events_complete(bot, trigger): + bot_events_recieved(bot, trigger.event) for comtype in bot.memory['SpiceBot_CommandsQuery']['commands'].keys(): if comtype not in ['module', 'nickname', 'rule']: bot_logging(bot, 'SpiceBot_CommandsQuery', "Found " + str(len(bot.memory['SpiceBot_CommandsQuery']['commands'][comtype].keys())) + " " + comtype + " commands.") - set_bot_event(bot, "SpiceBot_CommandsQuery") - - -@sopel.module.rule('^\?(.*)') -def query_detection(bot, trigger): - - while "SpiceBot_CommandsQuery" not in bot.memory: - pass - - # command must start with - if not str(trigger).startswith(tuple(['?'])): - return - - commands_list = dict() - for commandstype in bot.memory['SpiceBot_CommandsQuery']['commands'].keys(): - for com in bot.memory['SpiceBot_CommandsQuery']['commands'][commandstype].keys(): - if com not in commands_list.keys(): - commands_list[com] = bot.memory['SpiceBot_CommandsQuery']['commands'][commandstype][com] - - triggerargsarray = spicemanip.main(trigger, 'create') - - # command issued, check if valid - querycommand = spicemanip.main(triggerargsarray, 1).lower()[1:] - if len(querycommand) == 1: - commandlist = [] - for command in commands_list.keys(): - if command.lower().startswith(querycommand): - commandlist.append(command) - if commandlist == []: - bot.notice("No commands match " + str(querycommand) + ".", trigger.nick) - return - else: - bot.notice("The following commands match " + str(querycommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick) - return - - elif querycommand.endswith(tuple(["+"])): - querycommand = querycommand[:-1] - if querycommand not in commands_list.keys(): - bot.notice("The " + str(querycommand) + " does not appear to be valid.") - return - realcom = querycommand - if "aliasfor" in commands_list[querycommand].keys(): - realcom = commands_list[querycommand]["aliasfor"] - validcomlist = commands_list[realcom]["validcoms"] - bot.notice("The following commands match " + str(querycommand) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick) - return - - elif querycommand.endswith(tuple(['?'])): - querycommand = querycommand[:-1] - sim_com, sim_num = [], [] - for com in commands_list.keys(): - similarlevel = SequenceMatcher(None, querycommand.lower(), com.lower()).ratio() - sim_com.append(com) - sim_num.append(similarlevel) - sim_num, sim_com = (list(x) for x in zip(*sorted(zip(sim_num, sim_com), key=itemgetter(0)))) - closestmatch = spicemanip.main(sim_com, 'reverse', "list") - listnumb, relist = 1, [] - for item in closestmatch: - if listnumb <= 10: - relist.append(str(item)) - listnumb += 1 - bot.notice("The following commands may match " + str(querycommand) + ": " + spicemanip.main(relist, 'andlist') + ".", trigger.nick) - return - - elif querycommand in commands_list.keys(): - bot.notice("The following commands match " + str(querycommand) + ": " + str(querycommand) + ".", trigger.nick) - return - - elif not querycommand: - return - - else: - commandlist = [] - for command in commands_list.keys(): - if command.lower().startswith(querycommand): - commandlist.append(command) - if commandlist == []: - bot.notice("No commands match " + str(querycommand) + ".", trigger.nick) - return - else: - bot.notice("The following commands match " + str(querycommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick) - return - - -def commandsquery_register_type(bot, command_type): - if 'SpiceBot_CommandsQuery' not in bot.memory: - bot.memory['SpiceBot_CommandsQuery'] = dict() - - if 'counts' not in bot.memory['SpiceBot_CommandsQuery'].keys(): - bot.memory['SpiceBot_CommandsQuery']['counts'] = dict() - - if 'commands' not in bot.memory['SpiceBot_CommandsQuery'].keys(): - bot.memory['SpiceBot_CommandsQuery']['commands'] = dict() - - if command_type not in bot.memory['SpiceBot_CommandsQuery'].keys(): - if command_type not in bot.memory['SpiceBot_CommandsQuery']['counts'].keys(): - bot.memory['SpiceBot_CommandsQuery']['counts'][command_type] = 0 - if command_type not in bot.memory['SpiceBot_CommandsQuery']['commands'].keys(): - bot.memory['SpiceBot_CommandsQuery']['commands'][command_type] = dict() - bot.memory['SpiceBot_CommandsQuery']['counts'][command_type] += 1 - def commandsquery_register(bot, command_type, validcoms, aliasfor=None): if not isinstance(validcoms, list): validcoms = [validcoms] - if 'SpiceBot_CommandsQuery' not in bot.memory: - bot.memory['SpiceBot_CommandsQuery'] = dict() - - if 'counts' not in bot.memory['SpiceBot_CommandsQuery'].keys(): - bot.memory['SpiceBot_CommandsQuery']['counts'] = dict() - - if 'commands' not in bot.memory['SpiceBot_CommandsQuery'].keys(): - bot.memory['SpiceBot_CommandsQuery']['commands'] = dict() - if command_type not in bot.memory['SpiceBot_CommandsQuery'].keys(): if command_type not in bot.memory['SpiceBot_CommandsQuery']['counts'].keys(): bot.memory['SpiceBot_CommandsQuery']['counts'][command_type] = 0 @@ -309,3 +201,9 @@ def commandsquery_register(bot, command_type, validcoms, aliasfor=None): for comalias in comaliases: if comalias not in bot.memory['SpiceBot_CommandsQuery']['commands'][command_type].keys(): bot.memory['SpiceBot_CommandsQuery']['commands'][command_type][comalias] = {"aliasfor": aliasfor} + + +@sopel.module.event('2002') +@sopel.module.rule('.*') +def bot_events_setup(bot, trigger): + bot_events_recieved(bot, trigger.event) diff --git a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py new file mode 100644 index 0000000..14fceb6 --- /dev/null +++ b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py @@ -0,0 +1,90 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +import sopel +import sopel.module + +from difflib import SequenceMatcher +from operator import itemgetter + +from sopel_modules.SpiceBot_Events.System import bot_events_check + +import spicemanip + + +@sopel.module.rule('^\?(.*)') +def query_detection(bot, trigger): + + while not bot_events_check(bot, '2002'): + pass + + commands_list = dict() + for commandstype in bot.memory['SpiceBot_CommandsQuery']['commands'].keys(): + for com in bot.memory['SpiceBot_CommandsQuery']['commands'][commandstype].keys(): + if com not in commands_list.keys(): + commands_list[com] = bot.memory['SpiceBot_CommandsQuery']['commands'][commandstype][com] + + triggerargsarray = spicemanip.main(trigger, 'create') + + # command issued, check if valid + querycommand = spicemanip.main(triggerargsarray, 1).lower()[1:] + if len(querycommand) == 1: + commandlist = [] + for command in commands_list.keys(): + if command.lower().startswith(querycommand): + commandlist.append(command) + if commandlist == []: + bot.notice("No commands match " + str(querycommand) + ".", trigger.nick) + return + else: + bot.notice("The following commands match " + str(querycommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick) + return + + elif querycommand.endswith(tuple(["+"])): + querycommand = querycommand[:-1] + if querycommand not in commands_list.keys(): + bot.notice("The " + str(querycommand) + " does not appear to be valid.") + return + realcom = querycommand + if "aliasfor" in commands_list[querycommand].keys(): + realcom = commands_list[querycommand]["aliasfor"] + validcomlist = commands_list[realcom]["validcoms"] + bot.notice("The following commands match " + str(querycommand) + ": " + spicemanip.main(validcomlist, 'andlist') + ".", trigger.nick) + return + + elif querycommand.endswith(tuple(['?'])): + querycommand = querycommand[:-1] + sim_com, sim_num = [], [] + for com in commands_list.keys(): + similarlevel = SequenceMatcher(None, querycommand.lower(), com.lower()).ratio() + sim_com.append(com) + sim_num.append(similarlevel) + sim_num, sim_com = (list(x) for x in zip(*sorted(zip(sim_num, sim_com), key=itemgetter(0)))) + closestmatch = spicemanip.main(sim_com, 'reverse', "list") + listnumb, relist = 1, [] + for item in closestmatch: + if listnumb <= 10: + relist.append(str(item)) + listnumb += 1 + bot.notice("The following commands may match " + str(querycommand) + ": " + spicemanip.main(relist, 'andlist') + ".", trigger.nick) + return + + elif querycommand in commands_list.keys(): + bot.notice("The following commands match " + str(querycommand) + ": " + str(querycommand) + ".", trigger.nick) + return + + elif not querycommand: + return + + else: + commandlist = [] + for command in commands_list.keys(): + if command.lower().startswith(querycommand): + commandlist.append(command) + if commandlist == []: + bot.notice("No commands match " + str(querycommand) + ".", trigger.nick) + return + else: + bot.notice("The following commands match " + str(querycommand) + ": " + spicemanip.main(commandlist, 'andlist') + ".", trigger.nick) + return diff --git a/sopel_modules/SpiceBot_CommandsQuery/__init__.py b/sopel_modules/SpiceBot_CommandsQuery/__init__.py index c2c2e86..c439020 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/__init__.py +++ b/sopel_modules/SpiceBot_CommandsQuery/__init__.py @@ -6,6 +6,7 @@ Sopel Commands Query responds to ? to display commands matching patterns from __future__ import unicode_literals, absolute_import, division, print_function from .CommandsQuery import * +from . QueryCommand import * __author__ = 'Sam Zick' __email__ = 'sam@deathbybandaid.net' diff --git a/sopel_modules/SpiceBot_Events/Events.py b/sopel_modules/SpiceBot_Events/Events.py index 6316bd1..66d1451 100644 --- a/sopel_modules/SpiceBot_Events/Events.py +++ b/sopel_modules/SpiceBot_Events/Events.py @@ -53,6 +53,6 @@ def bot_events_connected(bot, trigger): @sopel.module.event('1004') @sopel.module.rule('.*') -def bot_events_monologue(bot, trigger): +def bot_events_complete(bot, trigger): bot_logging(bot, 'SpiceBot_Events', trigger.args[1]) bot_events_recieved(bot, trigger.event) diff --git a/sopel_modules/SpiceBot_GifSearch/gifsearch.py b/sopel_modules/SpiceBot_GifSearch/gifsearch.py index c68f958..52ff480 100644 --- a/sopel_modules/SpiceBot_GifSearch/gifsearch.py +++ b/sopel_modules/SpiceBot_GifSearch/gifsearch.py @@ -7,7 +7,7 @@ from sopel.config.types import StaticSection, ValidatedAttribute from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, startup_bot_event -from sopel_modules.SpiceBot_CommandsQuery.CommandsQuery import commandsquery_register, commandsquery_register_type +from sopel_modules.SpiceBot_CommandsQuery.CommandsQuery import commandsquery_register from sopel_modules.SpiceBot_SBTools import read_directory_json_to_dict, sopel_triggerargs, bot_logging @@ -90,7 +90,6 @@ def setup_thread(bot): valid_gif_api_dict[gif_api]["apikey"] = None bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][gif_api] = valid_gif_api_dict[gif_api] - commandsquery_register_type(bot, "prefix") for prefixcommand in bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys(): commandsquery_register(bot, "prefix", prefixcommand) diff --git a/sopel_modules/SpiceBot_SopelPatches/Patches.py b/sopel_modules/SpiceBot_SopelPatches/Patches.py new file mode 100644 index 0000000..90b1433 --- /dev/null +++ b/sopel_modules/SpiceBot_SopelPatches/Patches.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# coding=utf-8 +from __future__ import unicode_literals, absolute_import, print_function, division + +# sopel imports +import sopel.module + +from sopel_modules.SpiceBot_SBTools import bot_logging + + +def setup(bot): + bot_logging(bot, 'Sopel_Patch', "Patching Sopels built-in url_callbacks") + if 'url_callbacks' not in bot.memory: + bot.memory['url_callbacks'] = sopel.tools.SopelMemory() diff --git a/sopel_modules/SpiceBot_SopelPatches/__init__.py b/sopel_modules/SpiceBot_SopelPatches/__init__.py new file mode 100644 index 0000000..0232bc1 --- /dev/null +++ b/sopel_modules/SpiceBot_SopelPatches/__init__.py @@ -0,0 +1,8 @@ +# coding=utf8 +from __future__ import unicode_literals, absolute_import, division, print_function + +from .Patches import * + +__author__ = 'Sam Zick' +__email__ = 'sam@deathbybandaid.net' +__version__ = '0.1.1'