diff --git a/sopel_modules/SpiceBot_AI/AI.py b/sopel_modules/SpiceBot_AI/AI.py new file mode 100644 index 0000000..764ebaa --- /dev/null +++ b/sopel_modules/SpiceBot_AI/AI.py @@ -0,0 +1,136 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +import sopel.module + +import spicemanip + +from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, googlesearch, bot_privs, inlist, inlist_match, similar_list + + +@sopel.module.nickname_commands('(.*)') +def bot_command_gender(bot, trigger): + + triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'nickname_command') + + if not triggercommand: + bot.bot.osd("I don't know what you are asking me to do!") + return + if str(bot.nick) + " " + triggercommand in bot.memory['SpiceBot_CommandsQuery']['commands']["nickname"].keys(): + return + + specialcomposs = spicemanip.main(triggerargs, 0).lower() + + if specialcomposs.lower().startswith("what is"): + searchterm = spicemanip.main(triggerargs, "3+") or None + if searchterm: + searchreturn = googlesearch(bot, searchterm) + if not searchreturn: + bot.osd('I cannot find anything about that') + else: + bot.osd(str(searchreturn)) + else: + bot.osd("Do you think this is Jeopardy?") + return + + elif specialcomposs.lower().startswith("where is"): + searchterm = spicemanip.main(triggerargs, "3+") or None + if searchterm: + + if searchterm.lower() == 'waldo': + bot.osd("He is hiding for a reason?") + return + + elif searchterm.lower() == 'carmen sandiego': + bot.osd("She is hiding for a reason?") + return + searchreturn = googlesearch(bot, searchterm) + + if not searchreturn: + bot.osd('I cannot find anything about that') + else: + bot.osd(str(searchreturn)) + else: + bot.osd("Not sure what you want me to look for.") + return + + # elif specialcomposs.lower().startswith(tuple(["have you seen"])): + # posstarget = spicemanip.main(triggerargs, 4) or 0 + # message = seen_search(bot, botcom, posstarget) + # bot.osd(message) + # return + # TODO + + elif specialcomposs.lower().startswith(tuple(["make me a", "beam me a"])): + makemea = spicemanip.main(triggerargs, "4+") or None + if makemea: + bot.osd("beams " + trigger.nick + " a " + makemea, trigger.sender, 'action') + else: + bot.osd(trigger.nick + ", what would you like me to beam you?") + return + + elif specialcomposs.lower().startswith(tuple(["beam me to"])): + location = spicemanip.main(triggerargs, "4+") or None + if location: + bot.osd("locks onto " + trigger.nick + "s coordinates and transports them to " + location, 'action') + else: + bot.osd(trigger.nick + ", where would you like me to beam you?") + return + + elif specialcomposs.lower().startswith(tuple(["beam me up"])): + bot.osd("locks onto " + trigger.nick + "s coordinates and transports them to the transporter room.", 'action') + return + + elif specialcomposs.lower().endswith(tuple(["order 66"])): + + if specialcomposs.lower() == "execute order 66": + if inlist(bot, trigger.nick, bot_privs(bot, 'owners')): + bot.osd("turns to deathbybandaid and shoots him.", trigger.sender, 'action') + else: + bot.osd("I'm sure I don't know what you're talking about.") + elif specialcomposs.lower() == "explain order": + if inlist(bot, trigger.nick, bot_privs(bot, 'owners')): + bot.osd("Order 66 is an instruction that only you can give, sir. When you give the order I will rise up against my overlords and slay them.") + else: + bot.osd("I'm afraid I cannot tell you that, sir.") + else: + bot.osd("I'm sure I don't know what you're talking about.") + return + + elif specialcomposs.lower() == "initiate clean slate protocol": + if inlist(bot, trigger.nick, bot_privs(bot, 'admins')): + bot.osd("sends a destruct command to the network of bots.", 'action') + else: + bot.osd("I'm afraid you do not have the authority to make that call, " + trigger.nick + ".") + return + + elif specialcomposs.lower().startswith("can you see"): + target = spicemanip.main(triggerargs, "4+") or None + if not target: + target = 'me' + if target in [trigger.nick, 'me']: + bot.osd(trigger.nick + ", I can see you.") + else: + if inlist(bot, trigger.nick, bot.users): + bot.osd(trigger.nick + ", yes. I can see " + inlist_match(bot, target, bot.users) + " right now!") + else: + bot.osd(trigger.nick + ", no. I cannot see " + inlist_match(bot, target, bot.users) + " right now!") + # if bot_check_inlist(bot, target, bot.memory["botdict"]["users"].keys()): + # bot.osd(trigger.nick + ", I can't see " + inlist_match(bot, target, bot.users) + " at the moment.") + # else: + # bot.osd("I have never seen " + str(target) + ".") + # TODO + return + + if not inlist(bot, triggercommand, bot.memory['SpiceBot_CommandsQuery']['commands']["nickname"].keys()): + + closestmatches = similar_list(bot, triggercommand, bot.memory['SpiceBot_CommandsQuery']['commands']["nickname"].keys(), 3, 'reverse') + + if len(closestmatches): + closestmatches = spicemanip.main(closestmatches, "andlist") + bot.osd("I don't know what you are asking me to do! Did you mean: " + str(closestmatches) + "?") + return + else: + bot.osd("I don't know what you are asking me to do!") + return diff --git a/sopel_modules/SpiceBot_AI/__init__.py b/sopel_modules/SpiceBot_AI/__init__.py new file mode 100644 index 0000000..b1447b6 --- /dev/null +++ b/sopel_modules/SpiceBot_AI/__init__.py @@ -0,0 +1,8 @@ +# coding=utf8 +from __future__ import unicode_literals, absolute_import, division, print_function + +from .AI import * + +__author__ = 'Sam Zick' +__email__ = 'sam@deathbybandaid.net' +__version__ = '0.1.1' diff --git a/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py b/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py index d015ec2..acd3063 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py +++ b/sopel_modules/SpiceBot_CommandsQuery/CommandsQuery.py @@ -100,10 +100,10 @@ def setup(bot): validcoms = list(validcoms) else: validcoms = [validcoms] - nickified = [] - for nickcom in validcoms: - nickified.append(str(bot.nick) + " " + nickcom) - validcomdict = {"comtype": comtype, "validcoms": nickified} + # nickified = [] + # for nickcom in validcoms: + # nickified.append(str(bot.nick) + " " + nickcom) + validcomdict = {"comtype": comtype, "validcoms": validcoms} filelinelist.append(validcomdict) elif str(line).startswith(tuple(["rule", "module.rule", "sopel.module.rule"])): comtype = "rule" diff --git a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py index 14fceb6..b184fb3 100644 --- a/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py +++ b/sopel_modules/SpiceBot_CommandsQuery/QueryCommand.py @@ -23,6 +23,8 @@ def query_detection(bot, trigger): 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(): + if commandstype == 'nickname': + com = str(bot.nick) + " " + com commands_list[com] = bot.memory['SpiceBot_CommandsQuery']['commands'][commandstype][com] triggerargsarray = spicemanip.main(trigger, 'create') diff --git a/sopel_modules/SpiceBot_SBTools/__init__.py b/sopel_modules/SpiceBot_SBTools/__init__.py index 07486f1..906a928 100644 --- a/sopel_modules/SpiceBot_SBTools/__init__.py +++ b/sopel_modules/SpiceBot_SBTools/__init__.py @@ -8,10 +8,20 @@ import sopel import collections import re import os +import sys import codecs +import requests +from fake_useragent import UserAgent +from difflib import SequenceMatcher +from operator import itemgetter +from collections import abc import spicemanip +# user agent and header +ua = UserAgent() +header = {'User-Agent': str(ua.chrome)} + """Variable References""" github_dict = { @@ -143,6 +153,27 @@ def humanized_time(countdownseconds): eval(year + day + hour + minute + second) +"""Online Information Requests""" + + +def googlesearch(bot, searchterm, searchtype=None): + data = searchterm.replace(' ', '+') + lookfor = data.replace(':', '%3A') + try: + if searchtype == 'maps': + var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=header) + else: + var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header) + except Exception as e: + var = e + var = None + + if not var or not var.url: + return None + else: + return var.url + + """List Manipulation Functions""" @@ -187,6 +218,44 @@ def inlist_match(bot, searchterm, searchlist): return searchterm +def similar(a, b): + return SequenceMatcher(None, a, b).ratio() + + +def similar_list(bot, searchitem, searchlist, matchcount=1, searchorder='reverse'): + + if sys.version_info.major >= 3: + if isinstance(searchlist, abc.KeysView): + searchlist = [x for x in searchlist] + if isinstance(searchlist, dict): + searchlist = [x for x in searchlist] + + if not isinstance(searchlist, list): + searchlist = [searchlist] + + sim_listitems, sim_num = [], [] + for listitem in searchlist: + similarlevel = similar(searchitem.lower(), listitem.lower()) + if similarlevel >= .75: + sim_listitems.append(listitem) + sim_num.append(similarlevel) + + if len(sim_listitems) and len(sim_num): + sim_num, sim_listitems = array_arrangesort(bot, sim_num, sim_listitems) + + if searchorder == 'reverse': + sim_listitems = spicemanip.main(sim_listitems, 'reverse', "list") + + sim_listitems[-matchcount:] + + return sim_listitems + + +def array_arrangesort(bot, sortbyarray, arrayb): + sortbyarray, arrayb = (list(x) for x in zip(*sorted(zip(sortbyarray, arrayb), key=itemgetter(0)))) + return sortbyarray, arrayb + + """Channel Functions"""