Files
WorldCraft_Discord_Bot/cogs/quiz.py

161 lines
5.0 KiB
Python

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 *
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])
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 = (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
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):
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
userlinked = self.give_reward(message.author.id, self.question[3])
if userlinked:
await message.channel.send(f"{message.author.mention} Won 🎉🎉")
else:
await message.channel.send(f"{message.author.mention} Won 🎉🎉! To claim? Link your minecraft account")
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))