Merge pull request #27 from victormylle/quiz

Quiz
This commit is contained in:
victormylle
2020-11-10 19:01:51 +01:00
committed by GitHub
4 changed files with 128 additions and 68 deletions

View File

@@ -0,0 +1,63 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DatabaseConnection {
public String get_discord_id(Player p){
try (Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.251/worldcraft_discord", "worldcraft_discord", "aquev5vcwhLwTdRt")) {
PreparedStatement stmt = conn.prepareStatement("SELECT discordid FROM playerlinks WHERE minecraftUUID=?");
stmt.setString(1, p.getUniqueId().toString());
ResultSet res = stmt.executeQuery();
if (res.next()){
return res.getString("discordid");
}else{
return null;
}
}catch(Exception e){
System.out.println("Exception: " + e);
}
return null;
}
public int get_reward(String discordid){
try (Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.251/worldcraft_discord", "worldcraft_discord", "aquev5vcwhLwTdRt")) {
PreparedStatement stmt = conn.prepareStatement("SELECT toclaim FROM quizplayers WHERE discordid=?");
stmt.setString(1, discordid);
ResultSet res = stmt.executeQuery();
if (res.next()) {
return res.getInt("toclaim");
}else{
return -1;
}
}catch(Exception e){
System.out.println("Exception: " + e);
}
return -1;
}
public void remove_toclaim(String discordid){
try (Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.251/worldcraft_discord", "worldcraft_discord", "aquev5vcwhLwTdRt")) {
PreparedStatement stmt = conn.prepareStatement("UPDATE quizplayers SET toclaim = 0 WHERE discordid=?");
stmt.setString(1, discordid);
stmt.executeUpdate();
}catch(Exception e){
System.out.println("Exception: " + e);
}
}
}

View File

@@ -62,6 +62,14 @@ class PlayerLink(commands.Cog):
channelid = ctx.channel.id
authorid = ctx.author.id
dbLinker = PlayerDBLinker()
if dbLinker.discordidused(authorid):
await ctx.send(f"{ctx.author.mention}, your account is already linked!")
dbLinker.close()
return
code = get_random_string(8)

View File

@@ -12,43 +12,38 @@ class QuizQuestions(commands.Cog):
def __init__(self, client):
self.client = client
@commands.group(name="q&a",case_insensitive=True, invoke_without_command=True)
@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, 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 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)
await ctx.send("question id: " + str(q_id))
# except:
# await ctx.send("Something went wrong")
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)
await ctx.send("question removed")
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:
@@ -56,17 +51,25 @@ class QuizQuestions(commands.Cog):
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:
await ctx.send("No questions found")
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:
message = ""
embed = discord.Embed()
embed.set_author(name="Questions")
embed.colour = discord.Colour.orange()
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)
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:
@@ -75,7 +78,6 @@ class QuizQuestions(commands.Cog):
class Quiz(commands.Cog):
def __init__(self, client):
self.client = client
self.in_progress = False
#self.interval = (5, 10)
self.interval = (8*60, 30*60)
self.auto = False
@@ -101,59 +103,45 @@ class Quiz(commands.Cog):
async def ask_question(self):
self.question = self.get_random_question()
question = self.get_random_question()
self.increment_asked_count(self.question[0])
self.increment_asked_count(question[0])
channel = self.client.get_channel(constants.QuizChannelID)
self.in_progress = True
answer_timer = Timer(1, 0)
embed = discord.Embed()
#embed.set_author(name="Quiz")
embed.colour = discord.Colour.orange()
embed.add_field(name="Question:", value=f"{self.question[1]}", inline=False)
embed.add_field(name="Reward:", value=f"${self.question[3]}", inline=False)
self.embed = embed
embed.add_field(name="Question:", value=f"{question[1]}", inline=False)
embed.add_field(name="Reward:", value=f"${question[3]} in game", inline=False)
await channel.send(embed=embed)
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
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=60*10)
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()
embed.add_field(name="Answer:", value=f"{self.question[2]}", inline=False)
await channel.send(embed=embed)
@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
self.give_reward(message.author.id, self.question[3])
playerdblinker = PlayerDBLinker()
self.embed.colour = discord.Colour.green()
if playerdblinker.discordidused(message.author.id):
self.embed.add_field(name="Answer:", value=f"{self.question[2]}", inline=False)
self.embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False)
#self.embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft by using /redeem")
self.embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online")
await message.channel.send(embed=self.embed)
else:
self.embed.add_field(name="Answer:", value=f"{self.question[2]}", inline=False)
self.embed.add_field(name="Winner:", value=f"{message.author.mention} 🎉🎉", inline=False)
#self.embed.add_field(name="Claim:", value=f"1. Link your account by using /link <MinecraftName> \n2. Claim your reward in Minecraft by using /redeem")
self.embed.add_field(name="Claim:", value=f"Claim your reward in Minecraft when the server is online")
await message.channel.send(embed=self.embed)
playerdblinker.close()
def increment_asked_count(self, q_id):
quizdb = QuizDB()

View File

@@ -20,4 +20,5 @@ modPlusRoles = [roleAdmin, roleMod, roleOwner]
# Channels
ModLogs = 760807882899193867
#QuizChannelID_dev = 774418250665951232
QuizChannelID = 774418250665951232