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="questions",case_insensitive=True, invoke_without_command=True) @commands.check(checks.isModPlus) # /q&a add "What is ...." "Australia" async def questions(self, ctx, *args): pass @questions.command(name="add") @commands.check(checks.isModPlus) async def add_question(self, ctx, q, a, reward=50): try: quizdb = QuizDB() q_id = quizdb.add_question(q, a, reward) embed = discord.Embed() embed.colour = discord.Colour.green() embed.add_field(name="Success", value=f"Added question with {q_id}", inline=False) await ctx.send(embed=embed) except: await ctx.send("Something went wrong") finally: quizdb.close() @questions.command(name="rm") @commands.check(checks.isModPlus) async def rm_question(self, ctx, id): try: quizdb = QuizDB() quizdb.rm_question(id) embed = discord.Embed() embed.colour = discord.Colour.green() embed.add_field(name="Success", value=f"Question removed with id {id}", inline=False) await ctx.send(embed=embed) except QuestionNotFound: await ctx.send("No question found with id " + str(id)) # except: # await ctx.send("Something went wrong") finally: quizdb.close() @questions.command(name="show") @commands.check(checks.isModPlus) async def show_questions(self, ctx): try: quizdb = QuizDB() questions = quizdb.get_questions() if len(questions) == 0: embed = discord.Embed() embed.colour = discord.Colour.red() embed.add_field(name="Questions", value=f"No questions found", inline=False) await ctx.send(embed=embed) else: embed = discord.Embed() embed.set_author(name="Questions") embed.colour = discord.Colour.orange() for q in questions: embed.add_field(name=f"id: {q[0]}", value=f"Question: '{q[1]}'\nAnswer: '{q[2]}'\nReward: {q[3]}\nUsed: {q[4]}", inline=False) await ctx.send(embed=embed) except: await ctx.send("Something went wrong") finally: quizdb.close() @commands.group(name="wordgame",case_insensitive=True, invoke_without_command=True) @commands.check(checks.isModPlus) # /q&a add "What is ...." "Australia" async def wordgame(self, ctx, *args): pass @wordgame.command(name="add") @commands.check(checks.isModPlus) async def add_word(self, ctx, w, reward=50): try: wordgamedb = WordGameDB() w_id = wordgamedb.add_word(w, reward) embed = discord.Embed() embed.colour = discord.Colour.green() embed.add_field(name="Success", value=f"Added word with {w_id}", inline=False) await ctx.send(embed=embed) except: await ctx.send("Something went wrong") finally: wordgamedb.close() @wordgame.command(name="rm") @commands.check(checks.isModPlus) async def rm_word(self, ctx, id): try: wordgamedb = WordGameDB() wordgamedb.rm_word(id) embed = discord.Embed() embed.colour = discord.Colour.green() embed.add_field(name="Success", value=f"Question removed with id {id}", inline=False) await ctx.send(embed=embed) except QuestionNotFound: await ctx.send("No question found with id " + str(id)) except: await ctx.send("Something went wrong") finally: wordgamedb.close() @wordgame.command(name="show") @commands.check(checks.isModPlus) async def show_words(self, ctx): try: wordgamedb = WordGameDB() words = wordgamedb.get_words() if len(words) == 0: embed = discord.Embed() embed.colour = discord.Colour.red() embed.add_field(name="Words", value=f"No words found", inline=False) await ctx.send(embed=embed) else: embed = discord.Embed() embed.set_author(name="Words") embed.colour = discord.Colour.orange() for w in words: embed.add_field(name=f"id: {w['id']}", value=f"Word: '{w['word']}'\nReward: {w['reward']}\nUsed: {w['asked']}", inline=False) await ctx.send(embed=embed) except: await ctx.send("Something went wrong") finally: wordgamedb.close() class Quiz(commands.Cog): def __init__(self, client): self.client = client #self.interval = (5, 10) self.interval = (5*60, 20*60) self.auto = False self.timeout = 60*10 @commands.group(name="quiz",case_insensitive=True, invoke_without_command=True) @commands.check(checks.isModPlus) async def quiz(self, ctx, f, *args): pass @quiz.command(name="auto") @commands.check(checks.isModPlus) async def auto_quiz(self, ctx): self.auto = True asyncio.create_task(self.start_auto_quiz()) @quiz.command(name="stop") @commands.check(checks.isModPlus) async def auto_quiz(self, ctx): self.auto = False async def start_auto_quiz(self): while self.auto: await asyncio.sleep(random.randint(self.interval[0], self.interval[1])) if random.random() < 0.4: await self.start_word_game() else: await self.ask_question() @commands.Cog.listener() async def on_ready(self): self.auto = True asyncio.create_task(self.start_auto_quiz()) async def ask_question(self): question = self.get_random_question() self.increment_asked_count(question[0]) channel = self.client.get_channel(constants.QuizChannelID) embed = discord.Embed() #embed.set_author(name="Quiz") embed.colour = discord.Colour.orange() embed.add_field(name="Question:", value=f"{question[1]}", inline=False) embed.add_field(name="Reward:", value=f"${question[3]} in game", inline=False) embed.add_field(name="End:", value=f"The quiz will end in 10 minutes!", inline=False) await channel.send(embed=embed) embed = discord.Embed() embed.colour = discord.Colour.orange() embed.add_field(name="Question:", value=f"{question[1]}", inline=False) embed.add_field(name="Reward:", value=f"${question[3]} in game", inline=False) embed.add_field(name="Answer:", value=f"{question[2]}", inline=False) try: message = await self.client.wait_for("message", check=lambda message: message.content.lower() == question[2].lower(), timeout=self.timeout) self.give_reward(message.author.id, question[3]) playerdblinker = PlayerDBLinker() embed.colour = discord.Colour.green() if playerdblinker.discordidused(message.author.id): embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False) embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft by using /redeem") #embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online") await message.channel.send(embed=embed) else: embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False) embed.add_field(name="Claim:", value=f"1. Link your account by using /link \n2. Claim your reward in Minecraft by using /redeem") #embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online") await message.channel.send(embed=embed) playerdblinker.close() except asyncio.TimeoutError: await channel.send("Nobody found the correct answer!") embed.colour = discord.Colour.red() await channel.send(embed=embed) async def start_word_game(self): word = self.get_random_word() self.increment_asked_word(int(word["id"])) channel = self.client.get_channel(constants.QuizChannelID) embed = discord.Embed() #embed.set_author(name="Quiz") embed.colour = discord.Colour.orange() shaken_word = self.shake_word(word["word"].lower()) embed.add_field(name="Question:", value=f"Find the word from: {shaken_word}", inline=False) embed.add_field(name="Reward:", value=f"${word['reward']} in game", inline=False) embed.add_field(name="End:", value=f"The game will end in 10 minutes!", inline=False) await channel.send(embed=embed) embed = discord.Embed() embed.colour = discord.Colour.orange() embed.add_field(name="Question:", value=f"{shaken_word}", inline=False) embed.add_field(name="Reward:", value=f"${word['reward']} in game", inline=False) embed.add_field(name="Answer:", value=f"{word['word']}", inline=False) try: message = await self.client.wait_for("message", check=lambda message: message.content.lower() == word['word'].lower(), timeout=self.timeout) self.give_reward(message.author.id, word["reward"]) playerdblinker = PlayerDBLinker() embed.colour = discord.Colour.green() if playerdblinker.discordidused(message.author.id): embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False) embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft by using /redeem") #embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online") await message.channel.send(embed=embed) else: embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False) embed.add_field(name="Claim:", value=f"1. Link your account by using /link \n2. Claim your reward in Minecraft by using /redeem") #embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online") await message.channel.send(embed=embed) playerdblinker.close() except asyncio.TimeoutError: await channel.send("Nobody found the correct answer!") embed.colour = discord.Colour.red() await channel.send(embed=embed) def shake_word(self, word): word_list = list(word) random.shuffle(word_list) return "".join(word_list) 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 get_random_word(self): wordgamedb = WordGameDB() w = wordgamedb.get_random_word() wordgamedb.close() return w def increment_asked_word(self, w_id): wordgamedb = WordGameDB() wordgamedb.word_asked_increment(w_id) wordgamedb.close() 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))