diff --git a/bot.py b/bot.py index 43497d5..bc7d372 100644 --- a/bot.py +++ b/bot.py @@ -1,25 +1,69 @@ #!/usr/bin/env python3 import os +import re from dotenv import load_dotenv import discord +from quiz import Quiz + load_dotenv() TOKEN = os.getenv("DISCORD_TOKEN") GUILD = os.getenv("DISCORD_GUILD") # Zocken mit Heidi - class QuizClient(discord.Client): + def __init__(self): super().__init__( status="Shortening the Way 24/7", ) - self.prefix = "Heidi, " + self.prefix = "Quiz, " + self.prefix_regex = "^" + self.prefix + + self.channel = None + self.quiz = None + + self.triggers = {} # automatic actions + # Example: self.triggers[lambda m: "jeremy" in m.author.nick.lower()] = self.autoreact_to_jeremy + + self.matchers = {} # react to messages + self.matchers["init: .*"] = self.init_quiz + self.matchers["start"] = self.start_quiz + + ### Voicelines + ### Helpers ------------------------------------------------------------------------------------ + def _help_text(self): + """ + 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() + ] + + 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) + + return response + + def _match(self, matcher, message): + """ + Check if a string matches against prefix + matcher (case-insensitive) + """ + return re.match(self.prefix_regex + matcher, message.content, re.IGNORECASE) ### Events ------------------------------------------------------------------------------------- @@ -30,8 +74,70 @@ 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) + break + ### Commands ----------------------------------------------------------------------------------- + async def init_quiz(self, message): + """ + Quiz, init: [NAME] - Initialisiere ein neues Quiz. + """ + + # Set self.channel + if "quiz" not in message.channel.name: + await message.channel.send("Kein Quizchannel!") + return + + self.channel = message.channel + await self.channel.send("Quiz starting in channel " + self.channel.name) + await self.channel.send("-" * 50) + + # Set self.quiz + self.quiz = Quiz((message.content.split(": "))[1]) + + # Set self.players + await self.channel.send("Determining players:") + react_message = await self.channel.send("Hier mit individuellem Emoji reagieren, am Ende mit dem Haken bestätigen!") + await react_message.add_reaction("✅") + + def check(reaction, user): + return reaction.message == react_message and str(reaction.emoji) == "✅" and user != client.user + + await self.wait_for('reaction_add', check=check) + + # TODO: get players from emojis + await self.channel.send("yay") + + + async def start_quiz(self, message): + """ + Quiz, start - Starte das Quiz + """ + if self.quiz == None or self.channel == None: + await message.channel.send("Vorher init du kek") + return + + # Ablauf: + # - post question with green checkmark reaction + # - players post answers + # - answer multiple choice with a, b, c, d emojis + # - close question with checkmark + # - print answer with player emojis + # - automatic winner for number questions? robust? maybe only print but set manually. + # - set winners by choosing the right emoji + # + # + # - track the points and make graphs + + client = QuizClient() client.run(TOKEN) diff --git a/chalamet-1200x640.jpg b/chalamet-1200x640.jpg new file mode 100644 index 0000000..ac70bf1 Binary files /dev/null and b/chalamet-1200x640.jpg differ diff --git a/quiz.py b/quiz.py new file mode 100644 index 0000000..d4a604e --- /dev/null +++ b/quiz.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +class Quiz(object): + + def __init__(self, name): + self.questions = [] + + with open(name) as file: + for line in file: + question = tuple(string.strip() for string in tuple(line.split(" "))) + self.questions.append(question)