From 909f6a332be3e1b14d7e1934b9dca00911d9d895 Mon Sep 17 00:00:00 2001 From: victormylle Date: Sat, 7 Nov 2020 12:21:19 +0100 Subject: [PATCH] Added quiz cog --- cogs/quiz.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 cogs/quiz.py diff --git a/cogs/quiz.py b/cogs/quiz.py new file mode 100644 index 0000000..1bb1520 --- /dev/null +++ b/cogs/quiz.py @@ -0,0 +1,104 @@ +import discord +from discord.ext import commands +from data import constants +import asyncio +import threading +from data.DatabaseConnection import * + + +class QuizQuestions(commands.Cog): + def __init__(self, client): + self.client = client + + @commands.group(name="q&a",case_insensitive=True, invoke_without_command=True) + # /q&a add "What is ...." "Australia" + async def questions(self, ctx, f, *args): + if (f == "add"): + if (len(args) == 2): + await self.add_question(ctx, args[0], args[1]) + else: + await ctx.send("Wrong amount of arguments") + elif (f == "rm"): + if (len(args) == 1): + await self.rm_question(ctx, args[0]) + else: + await ctx.send("Wrong amount of arguments") + elif (f == "show"): + if (len(args) == 0): + await self.show_questions(ctx) + else: + await ctx.send("Wrong amount of arguments") + + async def add_question(self, ctx, q, a): + try: + quizdb = QuizDB() + q_id = quizdb.add_question(q, a) + await ctx.send("question id: " + str(q_id)) + except: + await ctx.send("Something went wrong") + finally: + quizdb.close() + + async def rm_question(self, ctx, id): + try: + quizdb = QuizDB() + quizdb.rm_question(id) + await ctx.send("question removed") + except QuestionNotFound: + await ctx.send("No question found with id " + str(id)) + except: + await ctx.send("Something went wrong") + finally: + quizdb.close() + + async def show_questions(self, ctx): + try: + quizdb = QuizDB() + questions = quizdb.get_questions() + if len(questions) == 0: + await ctx.send("No questions found") + else: + message = "" + for q in questions: + message += f"id: {q[0]}, question: '{q[1]}', answer: '{q[2]}', used: {q[3]}\n" + await ctx.send(message) + except: + await ctx.send("Something went wrong") + finally: + quizdb.close() + +class Quiz(commands.Cog): + def __init__(self, client): + self.client = client + self.in_progress = False + + + @commands.Cog.listener() + async def on_ready(self): + loop = asyncio.get_event_loop() + task = loop.create_task(self.ask_question()) + + async def ask_question(self): + self.question = self.get_random_question() + channel = self.client.get_channel(constants.QuizChannelID) + self.in_progress = True + await channel.send(self.question[1]) + + @commands.Cog.listener() + async def on_message(self, message): + if message.author == self.client.user: + return + if self.in_progress and message.channel.id == constants.QuizChannelID: + if message.content.lower() == self.question[2].lower(): + self.in_progress = False + await message.channel.send(f"{message.author.mention} Won 🎉🎉") + + def get_random_question(self): + quizdb = QuizDB() + return quizdb.get_random_question() + + + +def setup(client): + client.add_cog(QuizQuestions(client)) + client.add_cog(Quiz(client))