Started creating quiz reward system
This commit is contained in:
51
cogs/quiz.py
51
cogs/quiz.py
@@ -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()
|
||||
|
||||
@@ -40,6 +40,30 @@ class DatabaseConnection:
|
||||
def close(self):
|
||||
self.mydb.close()
|
||||
|
||||
class QuizPlayersDB:
|
||||
def __init__(self):
|
||||
self.tablename = "quizplayers"
|
||||
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("create table IF NOT EXISTS {} (discordid tinytext NOT NULL, wins int DEFAULT 0, rewards int DEFAULT 0, toclaim int DEFAULT 0)".format(self.tablename))
|
||||
|
||||
def player_exists(self, discordid):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"SELECT count(*) FROM {self.tablename} WHERE discordid={str(discordid)}"
|
||||
cursor.execute(sql)
|
||||
res = cursor.fetchone()
|
||||
return res[0] == 1
|
||||
|
||||
def player_won(self, discordid, reward):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
if self.player_exists(discordid):
|
||||
sql = f"UPDATE {self.tablename} SET wins = wins + 1, rewards = rewards + {str(reward)}, toclaim = toclaim + {str(reward)} WHERE id = {str(q_id)}"
|
||||
cursor.execute(sql)
|
||||
else:
|
||||
sql = f"INSERT INTO {self.tablename} (discordid, wins, rewards, toclaim) VALUES ({discordid}, 1, {reward}, {reward})"
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
|
||||
class QuizDB:
|
||||
def __init__(self):
|
||||
self.tablename = "questions"
|
||||
@@ -60,12 +84,12 @@ class QuizDB:
|
||||
|
||||
def rm_question(self, id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {} where id='{}'".format(self.tablename, str(id)))
|
||||
cursor.execute("select count(*) from {} where id={}".format(self.tablename, str(id)))
|
||||
res = cursor.fetchone()
|
||||
if count_questions == 1:
|
||||
sql = f"DELETE FROM {self.tablename} WHERE id = '{str(id)}'"
|
||||
if res[0] == 1:
|
||||
sql = f"DELETE FROM {self.tablename} WHERE id = {str(id)}"
|
||||
cursor.execute(sql)
|
||||
res = self.discorddbconn.get_db().commit()
|
||||
self.discorddbconn.get_db().commit()
|
||||
else:
|
||||
raise QuestionNotFound()
|
||||
|
||||
@@ -81,8 +105,15 @@ class QuizDB:
|
||||
res = cursor.fetchone()
|
||||
return res[0]
|
||||
|
||||
def question_asked_increment(self, q_id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"UPDATE {self.tablename} SET asked = asked + 1 WHERE id = {str(q_id)}"
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
|
||||
|
||||
def get_random_question(self):
|
||||
if self.count_questions != 0:
|
||||
if self.count_questions() != 0:
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
|
||||
cursor.execute(sql)
|
||||
|
||||
Reference in New Issue
Block a user