From 1a83ca60494f487f96ed6529af812a48499753f0 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 2 May 2019 20:50:22 -0400 Subject: [PATCH] Startuyp Monologue --- .../SpiceBot_CommandsQuery/CommandsQuery.py | 48 ++++++------ sopel_modules/SpiceBot_GifSearch/gif.py | 4 +- sopel_modules/SpiceBot_GifSearch/gifapi.py | 4 +- sopel_modules/SpiceBot_GifSearch/gifsearch.py | 74 +++++++++---------- .../StartupMonologue.py | 74 +++++++++++++++++++ .../SpiceBot_StartupMonologue/__init__.py | 12 +++ 6 files changed, 151 insertions(+), 65 deletions(-) create mode 100644 sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py create mode 100644 sopel_modules/SpiceBot_StartupMonologue/__init__.py diff --git a/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py b/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py index 27d1fe6..c20a53f 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py +++ b/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py @@ -22,17 +22,17 @@ def configure(config): def setup(bot): - stderr("[Sopel-CommandsQuery] Evaluating Core Commands List") + stderr("[SpiceBot_CommandsQuery] Evaluating Core Commands List") threading.Thread(target=setup_thread, args=(bot,)).start() def setup_thread(bot): - bot.memory['Sopel-CommandsQuery'] = dict() + bot.memory['SpiceBot_CommandsQuery'] = dict() for comtype in ['module', 'nickname', 'rule']: - bot.memory['Sopel-CommandsQuery'][comtype + "_commands"] = dict() - bot.memory['Sopel-CommandsQuery'][comtype + "_commands_count"] = 0 + bot.memory['SpiceBot_CommandsQuery'][comtype + "_commands"] = dict() + bot.memory['SpiceBot_CommandsQuery'][comtype + "_commands_count"] = 0 filepathlisting = [] @@ -54,7 +54,7 @@ def setup_thread(bot): pypi_modules_dir = os.path.join(plugin_dir, pathname) filepathlisting.append(pypi_modules_dir) except Exception as e: - stderr("[Sopel-CommandsQuery] sopel_modules not loaded :" + str(e)) + stderr("[SpiceBot_CommandsQuery] sopel_modules not loaded :" + str(e)) # Extra directories filepathlist = [] @@ -127,7 +127,7 @@ def setup_thread(bot): comtypedict = str(comtype + "_commands") - bot.memory['Sopel-CommandsQuery'][comtypedict + "_count"] += 1 + bot.memory['SpiceBot_CommandsQuery'][comtypedict + "_count"] += 1 # default command to filename if "validcoms" not in dict_from_file.keys(): @@ -139,21 +139,21 @@ def setup_thread(bot): else: comaliases = [] - bot.memory['Sopel-CommandsQuery'][comtypedict][maincom] = dict_from_file + bot.memory['SpiceBot_CommandsQuery'][comtypedict][maincom] = dict_from_file for comalias in comaliases: - if comalias not in bot.memory['Sopel-CommandsQuery'][comtypedict].keys(): - bot.memory['Sopel-CommandsQuery'][comtypedict][comalias] = {"aliasfor": maincom} + if comalias not in bot.memory['SpiceBot_CommandsQuery'][comtypedict].keys(): + bot.memory['SpiceBot_CommandsQuery'][comtypedict][comalias] = {"aliasfor": maincom} for comtype in ['module_commands', 'nickname_commands', 'rule_commands']: - stderr("[Sopel-CommandsQuery] Found " + str(len(bot.memory['Sopel-CommandsQuery'][comtype].keys())) + " " + comtype + " commands.") + stderr("[SpiceBot_CommandsQuery] Found " + str(len(bot.memory['SpiceBot_CommandsQuery'][comtype].keys())) + " " + comtype + " commands.") - set_bot_event(bot, "Sopel-CommandsQuery") + set_bot_event(bot, "SpiceBot_CommandsQuery") @sopel.module.rule('^\?(.*)') def query_detection(bot, trigger): - while "Sopel-CommandsQuery" not in bot.memory: + while "SpiceBot_CommandsQuery" not in bot.memory: pass # command must start with @@ -161,11 +161,11 @@ def query_detection(bot, trigger): return commands_list = dict() - for commandstype in bot.memory['Sopel-CommandsQuery'].keys(): + for commandstype in bot.memory['SpiceBot_CommandsQuery'].keys(): if not commandstype.endswith("_count"): - for com in bot.memory['Sopel-CommandsQuery'][commandstype].keys(): + for com in bot.memory['SpiceBot_CommandsQuery'][commandstype].keys(): if com not in commands_list.keys(): - commands_list[com] = bot.memory['Sopel-CommandsQuery'][commandstype][com] + commands_list[com] = bot.memory['SpiceBot_CommandsQuery'][commandstype][com] triggerargsarray = spicemanip.main(trigger, 'create') @@ -237,13 +237,13 @@ def commandsquery_register(bot, command_type, validcoms, aliasfor=None): if not isinstance(validcoms, list): validcoms = [validcoms] - if 'Sopel-CommandsQuery' not in bot.memory: - bot.memory['Sopel-CommandsQuery'] = dict() + if 'SpiceBot_CommandsQuery' not in bot.memory: + bot.memory['SpiceBot_CommandsQuery'] = dict() - if command_type not in bot.memory['Sopel-CommandsQuery'].keys(): - bot.memory['Sopel-CommandsQuery'][command_type] = dict() - bot.memory['Sopel-CommandsQuery'][command_type + "_count"] = 0 - bot.memory['Sopel-CommandsQuery'][command_type + "_count"] += 1 + if command_type not in bot.memory['SpiceBot_CommandsQuery'].keys(): + bot.memory['SpiceBot_CommandsQuery'][command_type] = dict() + bot.memory['SpiceBot_CommandsQuery'][command_type + "_count"] = 0 + bot.memory['SpiceBot_CommandsQuery'][command_type + "_count"] += 1 dict_from_file = dict() @@ -258,10 +258,10 @@ def commandsquery_register(bot, command_type, validcoms, aliasfor=None): comaliases = spicemanip.main(dict_from_file["validcoms"], '2+', 'list') else: comaliases = [] - bot.memory['Sopel-CommandsQuery'][command_type][maincom] = dict_from_file + bot.memory['SpiceBot_CommandsQuery'][command_type][maincom] = dict_from_file else: comaliases = validcoms for comalias in comaliases: - if comalias not in bot.memory['Sopel-CommandsQuery'][command_type].keys(): - bot.memory['Sopel-CommandsQuery'][command_type][comalias] = {"aliasfor": aliasfor} + if comalias not in bot.memory['SpiceBot_CommandsQuery'][command_type].keys(): + bot.memory['SpiceBot_CommandsQuery'][command_type][comalias] = {"aliasfor": aliasfor} diff --git a/sopel_modules/SpiceBot_GifSearch/gif.py b/sopel_modules/SpiceBot_GifSearch/gif.py index 1c3cc4b..a5f4724 100644 --- a/sopel_modules/SpiceBot_GifSearch/gif.py +++ b/sopel_modules/SpiceBot_GifSearch/gif.py @@ -16,7 +16,7 @@ import spicemanip @module.commands('gif') def gif_trigger(bot, trigger): - while "Sopel-GifSearch" not in bot.memory: + while "SpiceBot_GifSearch" not in bot.memory: pass triggerargs = sopel_triggerargs(bot, trigger) @@ -24,7 +24,7 @@ def gif_trigger(bot, trigger): return bot.osd("Please present a query to search.") query = spicemanip.main(triggerargs, 0) - searchapis = bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys() + searchapis = bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys() searchdict = {"query": query, "gifsearch": searchapis} gifdict = getGif(bot, searchdict) diff --git a/sopel_modules/SpiceBot_GifSearch/gifapi.py b/sopel_modules/SpiceBot_GifSearch/gifapi.py index 923c6e6..36e0b87 100644 --- a/sopel_modules/SpiceBot_GifSearch/gifapi.py +++ b/sopel_modules/SpiceBot_GifSearch/gifapi.py @@ -16,12 +16,12 @@ from sopel_modules.SpiceBot_SBTools import sopel_triggerargs @sopel.module.commands('(.*)') def gifapi_triggers(bot, trigger): - while "Sopel-GifSearch" not in bot.memory: + while "SpiceBot_GifSearch" not in bot.memory: pass triggerargs, prefixcommand = sopel_triggerargs(bot, trigger, 'prefix_command') - if prefixcommand not in bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys(): + if prefixcommand not in bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys(): return if triggerargs == []: diff --git a/sopel_modules/SpiceBot_GifSearch/gifsearch.py b/sopel_modules/SpiceBot_GifSearch/gifsearch.py index 3dbeaba..9d16369 100644 --- a/sopel_modules/SpiceBot_GifSearch/gifsearch.py +++ b/sopel_modules/SpiceBot_GifSearch/gifsearch.py @@ -41,11 +41,11 @@ class GifAPISection(StaticSection): def configure(config): moduledir = os.path.dirname(os.path.abspath(__file__)) api_dir = os.path.join(moduledir, 'gifapi') - valid_gif_api_dict = read_directory_json_to_dict([api_dir], "Gif API", "[Sopel-GifSearch] ") + valid_gif_api_dict = read_directory_json_to_dict([api_dir], "Gif API", "[SpiceBot_GifSearch] ") config.define_section("SopelGifSearch", GifAPIMainSection, validate=False) - config.SopelGifSearch.configure_setting('extra', 'Sopel-GifSearch API Extra directory') - config.SopelGifSearch.configure_setting('nsfw', 'Sopel-GifSearch API nsfw content') + config.SopelGifSearch.configure_setting('extra', 'SpiceBot_GifSearch API Extra directory') + config.SopelGifSearch.configure_setting('nsfw', 'SpiceBot_GifSearch API nsfw content') for gif_api in valid_gif_api_dict.keys(): config.define_section(gif_api, GifAPISection, validate=False) @@ -55,14 +55,14 @@ def configure(config): def setup(bot): - stderr("[Sopel-GifSearch] Starting Setup Procedure") + stderr("[SpiceBot_GifSearch] Starting Setup Procedure") threading.Thread(target=setup_thread, args=(bot,)).start() def setup_thread(bot): - bot.memory["Sopel-GifSearch"] = {"cache": {}, "badgiflinks": [], 'valid_gif_api_dict': {}} + bot.memory["SpiceBot_GifSearch"] = {"cache": {}, "badgiflinks": [], 'valid_gif_api_dict': {}} dir_to_scan = [] @@ -74,7 +74,7 @@ def setup_thread(bot): if bot.config.SopelGifSearch.extra: dir_to_scan.append(bot.config.SopelGifSearch.extra) - valid_gif_api_dict = read_directory_json_to_dict(dir_to_scan, "Gif API", "[Sopel-GifSearch] ") + valid_gif_api_dict = read_directory_json_to_dict(dir_to_scan, "Gif API", "[SpiceBot_GifSearch] ") for gif_api in valid_gif_api_dict.keys(): bot.config.define_section(gif_api, GifAPISection, validate=False) @@ -83,17 +83,17 @@ def setup_thread(bot): valid_gif_api_dict[gif_api]["apikey"] = apikey else: valid_gif_api_dict[gif_api]["apikey"] = None - bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][gif_api] = valid_gif_api_dict[gif_api] + bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][gif_api] = valid_gif_api_dict[gif_api] - set_bot_event(bot, "Sopel-GifSearch") + set_bot_event(bot, "SpiceBot_GifSearch") - while 'Sopel-CommandsQuery' not in bot.memory: + while 'SpiceBot_CommandsQuery' not in bot.memory: pass - for prefix_command in bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys(): + for prefix_command in bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys(): commandsquery_register(bot, "prefix_command", prefix_command) - stderr("[Sopel-CommandsQuery] Found " + str(len(bot.memory['Sopel-CommandsQuery']["prefix_command"].keys())) + " " + "prefix_command" + " commands.") + stderr("[SpiceBot_CommandsQuery] Found " + str(len(bot.memory['SpiceBot_CommandsQuery']["prefix_command"].keys())) + " " + "prefix_command" + " commands.") @sopel.module.commands('gif') @@ -103,7 +103,7 @@ def gif_trigger(bot, trigger): return bot.osd("Please present a query to search.") query = spicemanip.main(trigger.args[1], 0) - searchapis = bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys() + searchapis = bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys() searchdict = {"query": query, "gifsearch": searchapis} gifdict = getGif(bot, searchdict) @@ -139,7 +139,7 @@ def getGif(bot, searchdict): query_defaults = { "query": None, "searchnum": 'random', - "gifsearch": bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys(), + "gifsearch": bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys(), "gifsearchremove": ['gifme'], "searchlimit": 'default', "nsfw": False, @@ -164,13 +164,13 @@ def getGif(bot, searchdict): # set api usage if not isinstance(searchdict['gifsearch'], list): - if str(searchdict['gifsearch']) in bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys(): + if str(searchdict['gifsearch']) in bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys(): searchdict['gifsearch'] = [searchdict['gifsearch']] else: - searchdict['gifsearch'] = bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys() + searchdict['gifsearch'] = bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys() else: for apis in searchdict['gifsearch']: - if apis not in bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'].keys(): + if apis not in bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys(): searchdict['gifsearch'].remove(apis) # Verify search limit @@ -192,26 +192,26 @@ def getGif(bot, searchdict): for currentapi in searchdict['gifsearch']: # url base - url = str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['url']) + url = str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['url']) # query - url += str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['query']) + str(searchdict["searchquery"]) + url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['query']) + str(searchdict["searchquery"]) # limit - url += str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['limit']) + str(searchdict["searchlimit"]) + url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['limit']) + str(searchdict["searchlimit"]) # nsfw search? if searchdict['nsfw']: - url += str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['nsfw']) + url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['nsfw']) else: - url += str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['sfw']) + url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['sfw']) # api key - url += str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['key']) + str(bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['apikey']) + url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['key']) + str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['apikey']) - if currentapi not in bot.memory["Sopel-GifSearch"]['cache'].keys(): - bot.memory["Sopel-GifSearch"]['cache'][currentapi] = dict() + if currentapi not in bot.memory["SpiceBot_GifSearch"]['cache'].keys(): + bot.memory["SpiceBot_GifSearch"]['cache'][currentapi] = dict() - if str(searchdict["searchquery"]) not in bot.memory["Sopel-GifSearch"]['cache'][currentapi].keys(): - bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = [] + if str(searchdict["searchquery"]) not in bot.memory["SpiceBot_GifSearch"]['cache'][currentapi].keys(): + bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = [] - if bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] == []: + if bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] == []: try: page = requests.get(url, headers=header) @@ -222,11 +222,11 @@ def getGif(bot, searchdict): data = json.loads(urllib.request.urlopen(url).read().decode('utf-8')) - results = data[bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['results']] + results = data[bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['results']] resultsarray = [] for result in results: appendresult = False - cururl = result[bot.memory["Sopel-GifSearch"]['valid_gif_api_dict'][currentapi]['cururl']] + cururl = result[bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['cururl']] slashsplit = str(cururl).split("/") fileextension = slashsplit[-1] if not fileextension or fileextension == '': @@ -244,23 +244,23 @@ def getGif(bot, searchdict): # Create Temp dict for every result tempresultnum = 0 for tempresult in resultsarray: - if tempresult not in bot.memory["Sopel-GifSearch"]["badgiflinks"]: + if tempresult not in bot.memory["SpiceBot_GifSearch"]["badgiflinks"]: tempresultnum += 1 tempdict = dict() tempdict["returnnum"] = tempresultnum tempdict["returnurl"] = tempresult tempdict["gifapi"] = currentapi - bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])].append(tempdict) + bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])].append(tempdict) else: verifygoodlinks = [] - for gifresult in bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]: - if gifresult["returnurl"] not in bot.memory["Sopel-GifSearch"]["badgiflinks"]: + for gifresult in bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]: + if gifresult["returnurl"] not in bot.memory["SpiceBot_GifSearch"]["badgiflinks"]: verifygoodlinks.append(gifresult) - bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = verifygoodlinks + bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = verifygoodlinks - if bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] != []: - gifapiresults.extend(bot.memory["Sopel-GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]) + if bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] != []: + gifapiresults.extend(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]) if gifapiresults == []: return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"} @@ -279,7 +279,7 @@ def getGif(bot, searchdict): if gifpage and not str(gifpage.status_code).startswith(tuple(["4", "5"])): randombad = False else: - bot.memory["Sopel-GifSearch"]["badgiflinks"].append(gifdict["returnurl"]) + bot.memory["SpiceBot_GifSearch"]["badgiflinks"].append(gifdict["returnurl"]) newlist = [] for tempdict in gifapiresults: if tempdict["returnurl"] != gifdict["returnurl"]: diff --git a/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py b/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py new file mode 100644 index 0000000..81171a6 --- /dev/null +++ b/sopel_modules/SpiceBot_StartupMonologue/StartupMonologue.py @@ -0,0 +1,74 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +from sopel import module +from sopel.tools import stderr + +import sopel_modules.osd + + +from sopel_modules.SpiceBot_Botevents.BotEvents import set_bot_event, bot_startup_requirements_met, check_bot_events + +from sopel_modules.SpiceBot_CommandsQuery.CommandsQuery import commandsquery_register + + +def configure(config): + pass + + +def setup(bot): + pass + + +@module.event('001') +@module.rule('.*') +@module.thread(True) +def bot_startup_monologue(bot, trigger): + + while not check_bot_events(bot, ["connected"]): + pass + + if bot_startup_requirements_met(bot, ["SpiceBot_StartupMonologue"]): + startup_reconnect(bot, trigger) + else: + startup_fresh(bot, trigger) + + +def startup_fresh(bot, trigger): + + # Startup + stderr("[SpiceBot_StartupMonologue] " + bot.nick + " is now starting. Please wait while I load my configuration.") + bot.osd(" is now starting. Please wait while I load my configuration.", bot.channels.keys(), 'ACTION') + + startupcomplete = [bot.nick + " startup complete"] + + while not check_bot_events(bot, ["SpiceBot_CommandsQuery"]): + pass + + availablecomsnum, availablecomsfiles = 0, 0 + + for commandstype in bot.memory['SpiceBot_CommandsQuery'].keys(): + if commandstype.endswith("_count"): + availablecomsfiles += bot.memory['SpiceBot_CommandsQuery'][commandstype] + else: + availablecomsnum += len(bot.memory['SpiceBot_CommandsQuery'][commandstype].keys()) + + startupcomplete.append("There are " + str(availablecomsnum) + " commands available in " + str(availablecomsfiles) + " files.") + stderr("[SpiceBot_StartupMonologue] " + "There are " + str(availablecomsnum) + " commands available in " + str(availablecomsfiles) + " files.") + + while not check_bot_events(bot, ["startup_complete"]): + pass + + # Announce to chan, then handle some closing stuff + bot.osd(startupcomplete, bot.channels.keys()) + stderr("[SpiceBot_StartupMonologue] " + bot.nick + " startup complete") + + set_bot_event(bot, "SpiceBot_StartupMonologue") + + +def startup_reconnect(bot, trigger): + + # Startup + stderr("[SpiceBot_StartupMonologue] " + bot.nick + " has reconnected.") + bot.osd(" has reconnected.", bot.channels.keys(), 'ACTION') diff --git a/sopel_modules/SpiceBot_StartupMonologue/__init__.py b/sopel_modules/SpiceBot_StartupMonologue/__init__.py new file mode 100644 index 0000000..70dd200 --- /dev/null +++ b/sopel_modules/SpiceBot_StartupMonologue/__init__.py @@ -0,0 +1,12 @@ +# coding=utf8 +"""Sopel Startup Monologue + +Sopel Startup Monologue displays to all channels that the bot is online +""" +from __future__ import unicode_literals, absolute_import, division, print_function + +from .StartupMonologue import * + +__author__ = 'Sam Zick' +__email__ = 'sam@deathbybandaid.net' +__version__ = '0.1.0'