import discord from discord.ext import commands from data import constants import asyncio import threading import random from functions.timer import Timer from data.DatabaseConnection import * from functions import checks class QuizQuestions(commands.Cog): def __init__(self, client): self.client = client @commands.group(name="q&a",case_insensitive=True, invoke_without_command=True) @commands.check(checks.isModPlus) # /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]) elif (len(args) == 3): await self.add_question(ctx, args[0], args[1], args[2]) 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, reward=50): try: quizdb = QuizDB() q_id = quizdb.add_question(q, a, reward) 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]}', reward: {q[3]}, used: {q[4]}\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 #self.interval = (5, 10) self.interval = (8*60, 30*60) self.auto = False @commands.command(name="quiz") @commands.check(checks.isModPlus) async def quiz_bot(self, ctx, f, *args): if (f == "auto" and not self.auto): self.auto = True asyncio.create_task(self.start_auto_quiz()) elif (f == "stop" and self.auto): self.auto = False async def start_auto_quiz(self): while self.auto: await asyncio.sleep(random.randint(self.interval[0], self.interval[1])) await self.ask_question() @commands.Cog.listener() async def on_ready(self): self.auto = True casyncio.create_task(self.start_auto_quiz() async def ask_question(self): self.question = self.get_random_question() self.increment_asked_count(self.question[0]) channel = self.client.get_channel(constants.QuizChannelID) self.in_progress = True answer_timer = Timer(1, 0) embed = discord.Embed() #embed.set_author(name="Quiz") embed.colour = discord.Colour.orange() embed.add_field(name="Question:", value=f"{self.question[1]}", inline=False) embed.add_field(name="Reward:", value=f"${self.question[3]}", inline=False) self.embed = embed await channel.send(embed=embed) answer_timer.start() while not answer_timer.ended and self.in_progress: await asyncio.sleep(1) if (not answer_timer.ended): answer_timer.stop() else: self.in_progress = False embed.colour = discord.Colour.red() embed.add_field(name="Answer:", value=f"{self.question[2]}", inline=False) await channel.send(embed=embed) @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 self.give_reward(message.author.id, self.question[3]) playerdblinker = PlayerDBLinker() self.embed.colour = discord.Colour.green() if playerdblinker.discordidused(message.author.id): self.embed.add_field(name="Answer:", value=f"{self.question[2]}", inline=False) self.embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False) #self.embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft by using /redeem") self.embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online") await message.channel.send(embed=self.embed) else: self.embed.add_field(name="Answer:", value=f"{self.question[2]}", inline=False) self.embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False) #self.embed.add_field(name="Claim:", value=f"1. Link your account by using /link \n2. Claim your reward in Minecraft by using /redeem") self.embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online") await message.channel.send(embed=self.embed) playerdblinker.close() def increment_asked_count(self, q_id): quizdb = QuizDB() quizdb.question_asked_increment(q_id) quizdb.close() def get_random_question(self): quizdb = QuizDB() q = quizdb.get_random_question() quizdb.close() return q def give_reward(self, discordid, reward): quiz_players_db = QuizPlayersDB() quiz_players_db.player_won(discordid, reward) quiz_players_db.close() def setup(client): client.add_cog(QuizQuestions(client)) client.add_cog(Quiz(client))