diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..2395f76 --- /dev/null +++ b/config.json.example @@ -0,0 +1,7 @@ +{ + "llama_key": "Bearer $KEY", + "nick": "test_cube", + "channels": [ + "#test" + ] +} diff --git a/main.py b/main.py index 539a343..7789c6b 100644 --- a/main.py +++ b/main.py @@ -1,33 +1,20 @@ -# what you have: -# QuizBot: answer "true" or "false": tilde.town is primarily run by ~vilmibomb -# QuizBot: AI response: TRUE -# RealHuman: false -# QuizBot: the correct answer is FALSE! -# -# what I'd prefer: -# QuizBot: will LLM answer "correct" or "incorrect": tilde.town is primarily run by ~vilmibomb -# RealHuman: correct -# QuizBot: your guess is CORRECT! the LLM answered: "TRUE! tilde.town is run by vilmibm" - - from random import choice import requests from time import sleep import socket import re -from base64 import b64decode from json import dump, load, dumps try: - with open("secrets.json", "r") as f: - secrets = load(f) + with open("config.json", "r") as f: + config = load(f) except FileNotFoundError: - exit("Please create secrets.json with api key(s)") + exit("Please create config.json with api key(s) and channels") llama_url = "https://llama.mcopp.com/v1/chat/completions" llama_headers = { "Content-Type": "application/json", - "Authorization": secrets["llama_key"] + "Authorization": config["llama_key"] } channel_re = re.compile(r"PRIVMSG (#\w+)") @@ -36,13 +23,10 @@ llm_answer_re = re.compile(r"^(true|false)") host = "localhost" port = 6667 -nick = "the_cube" +nick = config["nick"] realname = "a bot by ~nebula" helptext = "!trivia, !trscores, !aitrivia, !aiscores for trivia game. contact ~nebula for help, feedback or problem reports." -channels = [ - "#tildetown", - "#bots" -] +channels = config["channels"] url = "https://opentdb.com/api.php?amount=50&type=boolean&encode=base64" questions_file = "trivia.questions" @@ -277,13 +261,14 @@ def llama_response(question): return response["choices"][0]["message"]["content"] class IRCBot(): - def __init__(self, nick, realname, helptext, commands, channels): + def __init__(self, nick, realname, helptext, commands, searchers, channels): self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s.connect((host, port)) self.nick = nick self.realname = realname self.helptext = helptext self.commands = commands + self.searchers = searchers self.channels = channels self.sendline(f"NICK {self.nick}") self.sendline(f"USER {self.nick} 0 * :{self.realname}") @@ -316,28 +301,38 @@ class IRCBot(): self.sendline(pong) continue channel_search = channel_re.search(line) - if channel_search: - channel = channel_search.group(1) - if line.endswith("!rollcall"): - self.send(channel, self.helptext) - continue - for command, callback in self.commands: - name_search = name_re.search(line) - if name_search: - name = name_search.group(1) - else: - name = None - if line.lower().endswith(command): - result = callback(channel, name) - if result: - self.send(channel, result) + if not channel_search: + continue + channel = channel_search.group(1) + try: + message_body = line[line.index(" :") + 2:] + except Exception as e: + print(e) + message_body = "" + if line.endswith("!rollcall"): + self.send(channel, self.helptext) + continue + for command, callback in self.commands: + name_search = name_re.search(line) + if name_search: + name = name_search.group(1) + else: + name = None + if line.lower().endswith(command): + result = callback(channel, name) + if result: + self.send(channel, result) + if message_body: + for callback in self.searchers: + callback(message_body) + def run(): bot = IRCBot( nick, realname, helptext, - [ + [ # endswith commands ("!trivia", post_question), ("!aitrivia", post_ai_question), ("!trscores", post_top_scores), @@ -347,6 +342,9 @@ def run(): ("right", answer_correct), ("wrong", answer_incorrect) ], + [ # message searchers + # empty + ], channels )