Files
WorldCraft_Discord_Bot/cogs/quiz.py
2020-11-14 13:38:36 +01:00

182 lines
6.5 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 *
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()
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]))
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 <MinecraftName> \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 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))