128
cogs/quiz.py
128
cogs/quiz.py
@@ -75,6 +75,71 @@ class QuizQuestions(commands.Cog):
|
||||
finally:
|
||||
quizdb.close()
|
||||
|
||||
|
||||
|
||||
@commands.group(name="wordgame",case_insensitive=True, invoke_without_command=True)
|
||||
@commands.check(checks.isModPlus)
|
||||
# /q&a add "What is ...." "Australia"
|
||||
async def wordgame(self, ctx, *args):
|
||||
pass
|
||||
|
||||
@wordgame.command(name="add")
|
||||
@commands.check(checks.isModPlus)
|
||||
async def add_word(self, ctx, w, reward=50):
|
||||
try:
|
||||
wordgamedb = WordGameDB()
|
||||
w_id = wordgamedb.add_word(w, reward)
|
||||
embed = discord.Embed()
|
||||
embed.colour = discord.Colour.green()
|
||||
embed.add_field(name="Success", value=f"Added word with {w_id}", inline=False)
|
||||
await ctx.send(embed=embed)
|
||||
except:
|
||||
await ctx.send("Something went wrong")
|
||||
finally:
|
||||
wordgamedb.close()
|
||||
|
||||
|
||||
@wordgame.command(name="rm")
|
||||
@commands.check(checks.isModPlus)
|
||||
async def rm_word(self, ctx, id):
|
||||
try:
|
||||
wordgamedb = WordGameDB()
|
||||
wordgamedb.rm_word(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:
|
||||
wordgamedb.close()
|
||||
|
||||
@wordgame.command(name="show")
|
||||
@commands.check(checks.isModPlus)
|
||||
async def show_words(self, ctx):
|
||||
try:
|
||||
wordgamedb = WordGameDB()
|
||||
words = wordgamedb.get_words()
|
||||
if len(words) == 0:
|
||||
embed = discord.Embed()
|
||||
embed.colour = discord.Colour.red()
|
||||
embed.add_field(name="Words", value=f"No words found", inline=False)
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
else:
|
||||
embed = discord.Embed()
|
||||
embed.set_author(name="Words")
|
||||
embed.colour = discord.Colour.orange()
|
||||
for w in words:
|
||||
embed.add_field(name=f"id: {w['id']}", value=f"Word: '{w['word']}'\nReward: {w['reward']}\nUsed: {w['asked']}", inline=False)
|
||||
await ctx.send(embed=embed)
|
||||
except:
|
||||
await ctx.send("Something went wrong")
|
||||
finally:
|
||||
wordgamedb.close()
|
||||
|
||||
class Quiz(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
@@ -104,7 +169,10 @@ class Quiz(commands.Cog):
|
||||
async def start_auto_quiz(self):
|
||||
while self.auto:
|
||||
await asyncio.sleep(random.randint(self.interval[0], self.interval[1]))
|
||||
await self.ask_question()
|
||||
if random.random() < 0.4:
|
||||
await self.start_word_game()
|
||||
else:
|
||||
await self.ask_question()
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
@@ -157,7 +225,54 @@ class Quiz(commands.Cog):
|
||||
embed.colour = discord.Colour.red()
|
||||
await channel.send(embed=embed)
|
||||
|
||||
async def start_word_game(self):
|
||||
word = self.get_random_word()
|
||||
self.increment_asked_word(int(word["id"]))
|
||||
channel = self.client.get_channel(constants.QuizChannelID)
|
||||
|
||||
embed = discord.Embed()
|
||||
#embed.set_author(name="Quiz")
|
||||
embed.colour = discord.Colour.orange()
|
||||
|
||||
shaken_word = self.shake_word(word["word"].lower())
|
||||
embed.add_field(name="Question:", value=f"Find the word from: {shaken_word}", inline=False)
|
||||
embed.add_field(name="Reward:", value=f"${word['reward']} in game", inline=False)
|
||||
embed.add_field(name="End:", value=f"The game 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"{shaken_word}", inline=False)
|
||||
embed.add_field(name="Reward:", value=f"${word['reward']} in game", inline=False)
|
||||
embed.add_field(name="Answer:", value=f"{word['word']}", inline=False)
|
||||
|
||||
try:
|
||||
message = await self.client.wait_for("message", check=lambda message: message.content.lower() == word['word'].lower(), timeout=self.timeout)
|
||||
|
||||
self.give_reward(message.author.id, word["id"])
|
||||
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 shake_word(self, word):
|
||||
word_list = list(word)
|
||||
random.shuffle(word_list)
|
||||
return "".join(word_list)
|
||||
|
||||
def increment_asked_count(self, q_id):
|
||||
quizdb = QuizDB()
|
||||
@@ -170,6 +285,17 @@ class Quiz(commands.Cog):
|
||||
quizdb.close()
|
||||
return q
|
||||
|
||||
def get_random_word(self):
|
||||
wordgamedb = WordGameDB()
|
||||
w = wordgamedb.get_random_word()
|
||||
wordgamedb.close()
|
||||
return w
|
||||
|
||||
def increment_asked_word(self, w_id):
|
||||
wordgamedb = WordGameDB()
|
||||
wordgamedb.word_asked_increment(w_id)
|
||||
wordgamedb.close()
|
||||
|
||||
def give_reward(self, discordid, reward):
|
||||
quiz_players_db = QuizPlayersDB()
|
||||
quiz_players_db.player_won(discordid, reward)
|
||||
|
||||
@@ -10,6 +10,8 @@ class PlayerNotLinked(Exception):
|
||||
pass
|
||||
class QuestionNotFound(Exception):
|
||||
pass
|
||||
class WordNotFound(Exception):
|
||||
pass
|
||||
class PlayerNotFound(Exception):
|
||||
pass
|
||||
|
||||
@@ -86,6 +88,67 @@ class QuizPlayersDB:
|
||||
def close(self):
|
||||
self.discorddbconn.close()
|
||||
|
||||
class WordGameDB:
|
||||
def __init__(self):
|
||||
self.tablename = "wordGame"
|
||||
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("create table IF NOT EXISTS {} (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, word text NOT NULL, reward int NOT NULL, asked int DEFAULT 0)".format(self.tablename))
|
||||
|
||||
def add_word(self, word, reward):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"insert into {self.tablename} (word, reward) VALUES ('{word}', {str(reward)})"
|
||||
try:
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
return cursor.lastrowid
|
||||
except:
|
||||
raise SQLInsertError()
|
||||
|
||||
def rm_word(self, id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {} where id={}".format(self.tablename, str(id)))
|
||||
res = cursor.fetchone()
|
||||
if res[0] == 1:
|
||||
sql = f"DELETE FROM {self.tablename} WHERE id = {str(id)}"
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
else:
|
||||
raise QuestionNotFound()
|
||||
|
||||
def get_words(self):
|
||||
# word, used, reward
|
||||
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
||||
sql = f"SELECT * FROM {self.tablename}"
|
||||
cursor.execute(sql)
|
||||
return list(cursor.fetchall())
|
||||
|
||||
def word_asked_increment(self, w_id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"UPDATE {self.tablename} SET asked = asked + 1 WHERE id = {str(w_id)}"
|
||||
cursor.execute(sql)
|
||||
self.discorddbconn.get_db().commit()
|
||||
|
||||
def count_words(self):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {}".format(self.tablename))
|
||||
res = cursor.fetchone()
|
||||
return res[0]
|
||||
|
||||
def get_random_word(self):
|
||||
if self.count_words() != 0:
|
||||
cursor = self.discorddbconn.get_cursor(dictionary=True)
|
||||
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
|
||||
cursor.execute(sql)
|
||||
return cursor.fetchone()
|
||||
else:
|
||||
raise WordNotFound()
|
||||
|
||||
def close(self):
|
||||
self.discorddbconn.close()
|
||||
|
||||
|
||||
|
||||
class QuizDB:
|
||||
def __init__(self):
|
||||
self.tablename = "questions"
|
||||
|
||||
@@ -22,4 +22,4 @@ modPlusRoles = [roleAdmin, roleMod, roleOwner]
|
||||
ModLogs = 760807882899193867
|
||||
#QuizChannelID_dev = 774418250665951232
|
||||
QuizChannelID = 775776550871498752
|
||||
DiscordLinkerID = 776944220119760927
|
||||
DiscordLinkerID = 776944220119760927
|
||||
|
||||
Reference in New Issue
Block a user