From 2a1e48eb1a1b93eac48c8efd63a57d1f0d7f22a0 Mon Sep 17 00:00:00 2001 From: churl Date: Wed, 19 Jan 2022 16:35:36 +0100 Subject: [PATCH] update bot --- bot.py | 16 ++-------------- quiz.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 quiz.py diff --git a/bot.py b/bot.py index c496141..d577038 100644 --- a/bot.py +++ b/bot.py @@ -10,7 +10,9 @@ from dotenv import load_dotenv from quiz import Quiz +# used to start the bot locally, for docker the variables have to be set when the container is run load_dotenv() + TOKEN = os.getenv("DISCORD_TOKEN") GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi @@ -31,9 +33,6 @@ class QuizClient(discord.Client): self.players = dict() self.scores = list() - self.triggers = {} # automatic actions - # Example: self.triggers[lambda m: "jeremy" in m.author.nick.lower()] = self.autoreact_to_jeremy - self.matchers = {"hilfe$": self.help, "init: .*": self.init_quiz, "start$": self.run_quiz, @@ -52,11 +51,6 @@ class QuizClient(discord.Client): if message.author == client.user: return - for trigger in self.triggers: - if trigger(message): - await self.triggers[trigger](message) - break - for matcher in self.matchers: if self._match(matcher, message): await self.matchers[matcher](message) @@ -68,9 +62,6 @@ class QuizClient(discord.Client): """ Generate help-string from docstrings of matchers and triggers """ - docstrings_triggers = [ - " - " + func.__doc__.strip() for func in self.triggers.values() - ] docstrings_matchers = [ " - " + func.__doc__.strip() for func in self.matchers.values() ] @@ -78,9 +69,6 @@ class QuizClient(discord.Client): response = 'Präfix: "' + self.prefix + '" (mit Leerzeichen)\n' response += "--------------------------------------------------\n" - response += "Passiert automatisch:\n" - response += "\n".join(docstrings_triggers) - response += "\n\nEs gibt diese Befehle:\n" response += "\n".join(docstrings_matchers) diff --git a/quiz.py b/quiz.py new file mode 100644 index 0000000..185fc8b --- /dev/null +++ b/quiz.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +class Quiz(object): + + def _read_quiz(self, file): + for line in file: + if len(line.strip()) == 0: + continue + + question = tuple(string.strip() for string in tuple(line.split(" "))) + if question[1].startswith("{"): # handle multiple choice + question = (question[0], tuple(((question[1])[1:-1]).split(", ")), question[2]) + question = (question[0], question[1], tuple(((question[2])[1:-1]).split(": "))) + self.questions.append(question) + + def __init__(self, name): + self.name = name + self.questions = [] + + try: + with open(name) as file: # local call, file is in same directory + self._read_quiz(file) + print("Quiz read from ./" + name + "!") + except FileNotFoundError: + try: + with open("/quiz/" + name) as file: # docker call + self._read_quiz(file) + print("Quiz read from /quiz/" + name + "!") + except FileNotFoundError: + print("Error, file not found!") + + def __iter__(self): + def questions_gen(questions): + current = 0 + + while current < len(questions): + yield questions[current] + current += 1 + + return questions_gen(self.questions)