update bot
This commit is contained in:
16
bot.py
16
bot.py
@ -10,7 +10,9 @@ from dotenv import load_dotenv
|
|||||||
|
|
||||||
from quiz import Quiz
|
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()
|
load_dotenv()
|
||||||
|
|
||||||
TOKEN = os.getenv("DISCORD_TOKEN")
|
TOKEN = os.getenv("DISCORD_TOKEN")
|
||||||
GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi
|
GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi
|
||||||
|
|
||||||
@ -31,9 +33,6 @@ class QuizClient(discord.Client):
|
|||||||
self.players = dict()
|
self.players = dict()
|
||||||
self.scores = list()
|
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,
|
self.matchers = {"hilfe$": self.help,
|
||||||
"init: .*": self.init_quiz,
|
"init: .*": self.init_quiz,
|
||||||
"start$": self.run_quiz,
|
"start$": self.run_quiz,
|
||||||
@ -52,11 +51,6 @@ class QuizClient(discord.Client):
|
|||||||
if message.author == client.user:
|
if message.author == client.user:
|
||||||
return
|
return
|
||||||
|
|
||||||
for trigger in self.triggers:
|
|
||||||
if trigger(message):
|
|
||||||
await self.triggers[trigger](message)
|
|
||||||
break
|
|
||||||
|
|
||||||
for matcher in self.matchers:
|
for matcher in self.matchers:
|
||||||
if self._match(matcher, message):
|
if self._match(matcher, message):
|
||||||
await self.matchers[matcher](message)
|
await self.matchers[matcher](message)
|
||||||
@ -68,9 +62,6 @@ class QuizClient(discord.Client):
|
|||||||
"""
|
"""
|
||||||
Generate help-string from docstrings of matchers and triggers
|
Generate help-string from docstrings of matchers and triggers
|
||||||
"""
|
"""
|
||||||
docstrings_triggers = [
|
|
||||||
" - " + func.__doc__.strip() for func in self.triggers.values()
|
|
||||||
]
|
|
||||||
docstrings_matchers = [
|
docstrings_matchers = [
|
||||||
" - " + func.__doc__.strip() for func in self.matchers.values()
|
" - " + 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 = 'Präfix: "' + self.prefix + '" (mit Leerzeichen)\n'
|
||||||
response += "--------------------------------------------------\n"
|
response += "--------------------------------------------------\n"
|
||||||
|
|
||||||
response += "Passiert automatisch:\n"
|
|
||||||
response += "\n".join(docstrings_triggers)
|
|
||||||
|
|
||||||
response += "\n\nEs gibt diese Befehle:\n"
|
response += "\n\nEs gibt diese Befehle:\n"
|
||||||
response += "\n".join(docstrings_matchers)
|
response += "\n".join(docstrings_matchers)
|
||||||
|
|
||||||
|
40
quiz.py
Normal file
40
quiz.py
Normal 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)
|
Reference in New Issue
Block a user