update bot

This commit is contained in:
churl
2022-01-19 16:35:36 +01:00
parent 788a491869
commit 2a1e48eb1a
2 changed files with 42 additions and 14 deletions

16
bot.py
View File

@ -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)

40
quiz.py Normal file
View File

@ -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)