Started creating quiz reward system

This commit is contained in:
2020-11-08 01:16:24 +01:00
parent 73dfe3abf3
commit 24d0b05cef
2 changed files with 81 additions and 11 deletions

View File

@@ -3,6 +3,8 @@ from discord.ext import commands
from data import constants
import asyncio
import threading
import random
from functions.timer import Timer
from data.DatabaseConnection import *
@@ -46,8 +48,8 @@ class QuizQuestions(commands.Cog):
await ctx.send("question removed")
except QuestionNotFound:
await ctx.send("No question found with id " + str(id))
except:
await ctx.send("Something went wrong")
# except:
# await ctx.send("Something went wrong")
finally:
quizdb.close()
@@ -71,18 +73,47 @@ class Quiz(commands.Cog):
def __init__(self, client):
self.client = client
self.in_progress = False
self.interval = (5, 10)
#self.interval = (6*60, 30*60)
self.auto = False
@commands.command(name="quiz")
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
@commands.Cog.listener()
async def on_ready(self):
loop = asyncio.get_event_loop()
task = loop.create_task(self.ask_question())
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):
# loop = asyncio.get_event_loop()
# task = loop.create_task(self.ask_question())
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(10, 0)
await channel.send(self.question[1])
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
await channel.send("The answer was : " + self.question[2])
@commands.Cog.listener()
async def on_message(self, message):
@@ -91,8 +122,16 @@ class Quiz(commands.Cog):
if self.in_progress and message.channel.id == constants.QuizChannelID:
if message.content.lower() == self.question[2].lower():
self.in_progress = False
quiz_players_db = QuizPlayersDB()
quiz_players_db.player_won(message.author.id, 1)
await message.channel.send(f"{message.author.mention} Won 🎉🎉")
def increment_asked_count(self, q_id):
quizdb = QuizDB()
quizdb.question_asked_increment(q_id)
def get_random_question(self):
quizdb = QuizDB()
return quizdb.get_random_question()