refactoring stuff

main
nebula 2024-12-15 15:06:47 -06:00
parent 92d925613f
commit 20f97a4e8a
1 changed files with 59 additions and 57 deletions

116
main.py
View File

@ -11,53 +11,53 @@ try:
except FileNotFoundError: except FileNotFoundError:
exit("Please create config.json with api key(s) and channels") exit("Please create config.json with api key(s) and channels")
host = "localhost"
port = 6667
nick = config["nick"]
realname = "a bot by ~nebula"
helptext = "!trivia, !trscores, !aitrivia, !aiscores for trivia game. contact ~nebula for help, feedback or problem reports. https://git.tilde.town/nebula/mysterious_cube"
channels = config["channels"]
channel_re = re.compile(r"PRIVMSG (#*\w+)")
name_re = re.compile(r"^:([^!]*)!")
llm_answer_re = re.compile(r"^(true|false)")
llama_url = "https://llama.mcopp.com/v1/chat/completions" llama_url = "https://llama.mcopp.com/v1/chat/completions"
llama_headers = { llama_headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": config["llama_key"] "Authorization": config["llama_key"]
} }
channel_re = re.compile(r"PRIVMSG (#*\w+)") trivia_questions_file = "trivia.questions"
name_re = re.compile(r"^:([^!]*)!") trivia_state_file = "trivia.state"
llm_answer_re = re.compile(r"^(true|false)") trivia_score_file = "trivia.scores"
trivia_unselected_file = "trivia.unselected"
host = "localhost"
port = 6667
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 = config["channels"]
questions_file = "trivia.questions"
state_file = "trivia.state"
score_file = "trivia.scores"
unselected_file = "trivia.unselected"
ai_state_file = "trivia.aistate" ai_state_file = "trivia.aistate"
ai_score_file = "trivia.aiscores" ai_score_file = "trivia.aiscores"
try: try:
with open(questions_file, "r") as f: with open(trivia_questions_file, "r") as f:
questions = load(f) trivia_questions = load(f)
except FileNotFoundError: except FileNotFoundError:
questions = [] trivia_questions = []
try: try:
with open(state_file, "r") as f: with open(trivia_state_file, "r") as f:
state = load(f) trivia_state = load(f)
except FileNotFoundError: except FileNotFoundError:
state = {} trivia_state = {}
try: try:
with open(score_file, "r") as f: with open(trivia_score_file, "r") as f:
scores = load(f) trivia_scores = load(f)
except FileNotFoundError: except FileNotFoundError:
scores = {} trivia_scores = {}
try: try:
with open(unselected_file, "r") as f: with open(trivia_unselected_file, "r") as f:
unselected = load(f) trivia_unselected = load(f)
except FileNotFoundError: except FileNotFoundError:
unselected = [] trivia_unselected = []
try: try:
with open(ai_state_file, "r") as f: with open(ai_state_file, "r") as f:
@ -72,25 +72,25 @@ except FileNotFoundError:
ai_scores = {} ai_scores = {}
def write_state(): def write_state():
with open(state_file, "w") as f: with open(trivia_state_file, "w") as f:
dump(state, f) dump(trivia_state, f)
with open(score_file, "w") as f: with open(trivia_score_file, "w") as f:
dump(scores, f) dump(trivia_scores, f)
with open(unselected_file, "w") as f: with open(trivia_unselected_file, "w") as f:
dump(unselected, f) dump(trivia_unselected, f)
with open(ai_score_file, "w") as f: with open(ai_score_file, "w") as f:
dump(ai_scores, f) dump(ai_scores, f)
with open(ai_state_file, "w") as f: with open(ai_state_file, "w") as f:
dump(ai_state, f) dump(ai_state, f)
def get_question(ai_enabled=False): def get_question(ai_enabled=False):
global questions global trivia_questions
global unselected global trivia_unselected
if questions: if trivia_questions:
if not unselected: if not trivia_unselected:
unselected = questions.copy() trivia_unselected = trivia_questions.copy()
question = choice(unselected) question = choice(trivia_unselected)
unselected.remove(question) trivia_unselected.remove(question)
question.append(ai_enabled) question.append(ai_enabled)
# print(len(unselected)) # print(len(unselected))
return question return question
@ -98,10 +98,10 @@ def get_question(ai_enabled=False):
return False return False
def post_question(channel, username): def post_question(channel, username):
global state global trivia_state
question = get_question() question = get_question()
if question: if question:
state[channel] = question trivia_state[channel] = question
write_state() write_state()
return f"Answer 'true' or 'false': {question[0]}" return f"Answer 'true' or 'false': {question[0]}"
else: else:
@ -170,29 +170,29 @@ def ai_answer(choice, channel, name):
] ]
def answer(choice, channel, name): def answer(choice, channel, name):
global state global trivia_state
if channel not in state.keys(): if channel not in trivia_state.keys():
return None return None
_, answer, ai_enabled = state[channel] _, answer, ai_enabled = trivia_state[channel]
del state[channel] del trivia_state[channel]
write_state() write_state()
line = f"The answer is {answer}!" line = f"The answer is {answer}!"
if not ai_enabled and name: if not ai_enabled and name:
if name not in scores.keys(): if name not in trivia_scores.keys():
scores[name] = 0 trivia_scores[name] = 0
if choice == answer: if choice == answer:
scores[name] += 1 trivia_scores[name] += 1
line += f" {name} scores 1 point! Total score for {name}: {scores[name]}pts." line += f" {name} scores 1 point! Total score for {name}: {trivia_scores[name]}pts."
else: else:
scores[name] -= 1 trivia_scores[name] -= 1
line += f" {name} loses 1 point! Total score for {name}: {scores[name]}pts." line += f" {name} loses 1 point! Total score for {name}: {trivia_scores[name]}pts."
write_state() write_state()
line += " See top scores with !trscores" line += " See top scores with !trscores"
return line return line
def post_top_scores(channel, name): def post_top_scores(channel, name):
global scores global trivia_scores
score_list = [(name, score) for name, score in scores.items()] score_list = [(name, score) for name, score in trivia_scores.items()]
if not score_list: if not score_list:
return "No current scores." return "No current scores."
sorted_scores = sorted(score_list, key=lambda x: x[1], reverse=True) sorted_scores = sorted(score_list, key=lambda x: x[1], reverse=True)
@ -220,6 +220,9 @@ def post_top_ai_scores(channel, name):
count += 1 count += 1
return line[:-2] return line[:-2]
def post_help(channel, name):
return helptext
def answer_true(channel, name): def answer_true(channel, name):
return answer("true", channel, name) return answer("true", channel, name)
@ -310,9 +313,6 @@ class IRCBot():
except Exception as e: except Exception as e:
print(e) print(e)
message_body = "" message_body = ""
if line.endswith("!rollcall"):
self.send(channel, self.helptext)
continue
for command, callback in self.commands: for command, callback in self.commands:
if line.lower().endswith(command): if line.lower().endswith(command):
result = callback(channel, name) result = callback(channel, name)
@ -328,6 +328,8 @@ def run():
realname, realname,
helptext, helptext,
[ # endswith commands [ # endswith commands
("!help", post_help),
("!rollcall", post_help)
("!trivia", post_question), ("!trivia", post_question),
("!aitrivia", post_ai_question), ("!aitrivia", post_ai_question),
("!trscores", post_top_scores), ("!trscores", post_top_scores),