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:
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_headers = {
"Content-Type": "application/json",
"Authorization": config["llama_key"]
}
channel_re = re.compile(r"PRIVMSG (#*\w+)")
name_re = re.compile(r"^:([^!]*)!")
llm_answer_re = re.compile(r"^(true|false)")
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"
trivia_questions_file = "trivia.questions"
trivia_state_file = "trivia.state"
trivia_score_file = "trivia.scores"
trivia_unselected_file = "trivia.unselected"
ai_state_file = "trivia.aistate"
ai_score_file = "trivia.aiscores"
try:
with open(questions_file, "r") as f:
questions = load(f)
with open(trivia_questions_file, "r") as f:
trivia_questions = load(f)
except FileNotFoundError:
questions = []
trivia_questions = []
try:
with open(state_file, "r") as f:
state = load(f)
with open(trivia_state_file, "r") as f:
trivia_state = load(f)
except FileNotFoundError:
state = {}
trivia_state = {}
try:
with open(score_file, "r") as f:
scores = load(f)
with open(trivia_score_file, "r") as f:
trivia_scores = load(f)
except FileNotFoundError:
scores = {}
trivia_scores = {}
try:
with open(unselected_file, "r") as f:
unselected = load(f)
with open(trivia_unselected_file, "r") as f:
trivia_unselected = load(f)
except FileNotFoundError:
unselected = []
trivia_unselected = []
try:
with open(ai_state_file, "r") as f:
@ -72,25 +72,25 @@ except FileNotFoundError:
ai_scores = {}
def write_state():
with open(state_file, "w") as f:
dump(state, f)
with open(score_file, "w") as f:
dump(scores, f)
with open(unselected_file, "w") as f:
dump(unselected, f)
with open(trivia_state_file, "w") as f:
dump(trivia_state, f)
with open(trivia_score_file, "w") as f:
dump(trivia_scores, f)
with open(trivia_unselected_file, "w") as f:
dump(trivia_unselected, f)
with open(ai_score_file, "w") as f:
dump(ai_scores, f)
with open(ai_state_file, "w") as f:
dump(ai_state, f)
def get_question(ai_enabled=False):
global questions
global unselected
if questions:
if not unselected:
unselected = questions.copy()
question = choice(unselected)
unselected.remove(question)
global trivia_questions
global trivia_unselected
if trivia_questions:
if not trivia_unselected:
trivia_unselected = trivia_questions.copy()
question = choice(trivia_unselected)
trivia_unselected.remove(question)
question.append(ai_enabled)
# print(len(unselected))
return question
@ -98,10 +98,10 @@ def get_question(ai_enabled=False):
return False
def post_question(channel, username):
global state
global trivia_state
question = get_question()
if question:
state[channel] = question
trivia_state[channel] = question
write_state()
return f"Answer 'true' or 'false': {question[0]}"
else:
@ -170,29 +170,29 @@ def ai_answer(choice, channel, name):
]
def answer(choice, channel, name):
global state
if channel not in state.keys():
global trivia_state
if channel not in trivia_state.keys():
return None
_, answer, ai_enabled = state[channel]
del state[channel]
_, answer, ai_enabled = trivia_state[channel]
del trivia_state[channel]
write_state()
line = f"The answer is {answer}!"
if not ai_enabled and name:
if name not in scores.keys():
scores[name] = 0
if name not in trivia_scores.keys():
trivia_scores[name] = 0
if choice == answer:
scores[name] += 1
line += f" {name} scores 1 point! Total score for {name}: {scores[name]}pts."
trivia_scores[name] += 1
line += f" {name} scores 1 point! Total score for {name}: {trivia_scores[name]}pts."
else:
scores[name] -= 1
line += f" {name} loses 1 point! Total score for {name}: {scores[name]}pts."
trivia_scores[name] -= 1
line += f" {name} loses 1 point! Total score for {name}: {trivia_scores[name]}pts."
write_state()
line += " See top scores with !trscores"
return line
def post_top_scores(channel, name):
global scores
score_list = [(name, score) for name, score in scores.items()]
global trivia_scores
score_list = [(name, score) for name, score in trivia_scores.items()]
if not score_list:
return "No current scores."
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
return line[:-2]
def post_help(channel, name):
return helptext
def answer_true(channel, name):
return answer("true", channel, name)
@ -310,9 +313,6 @@ class IRCBot():
except Exception as e:
print(e)
message_body = ""
if line.endswith("!rollcall"):
self.send(channel, self.helptext)
continue
for command, callback in self.commands:
if line.lower().endswith(command):
result = callback(channel, name)
@ -328,6 +328,8 @@ def run():
realname,
helptext,
[ # endswith commands
("!help", post_help),
("!rollcall", post_help)
("!trivia", post_question),
("!aitrivia", post_ai_question),
("!trscores", post_top_scores),