Added ability to save and remove questions
This commit is contained in:
@@ -8,6 +8,8 @@ class UnLinkError(Exception):
|
||||
pass
|
||||
class PlayerNotLinked(Exception):
|
||||
pass
|
||||
class QuestionNotFound(Exception):
|
||||
pass
|
||||
|
||||
class DatabaseConnection:
|
||||
# databases -> worldcraft and worldcraft_discord
|
||||
@@ -38,6 +40,61 @@ class DatabaseConnection:
|
||||
def close(self):
|
||||
self.mydb.close()
|
||||
|
||||
class QuizDB:
|
||||
def __init__(self):
|
||||
self.tablename = "questions"
|
||||
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, question text NOT NULL, answer text NOT NULL, asked int DEFAULT 0)".format(self.tablename))
|
||||
|
||||
def add_question(self, question, answer):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = "insert into {} (question, answer) VALUES (%s, %s)".format(self.tablename)
|
||||
val = (question, answer)
|
||||
try:
|
||||
cursor.execute(sql, val)
|
||||
self.discorddbconn.get_db().commit()
|
||||
return cursor.lastrowid
|
||||
except:
|
||||
raise SQLInsertError()
|
||||
|
||||
def rm_question(self, id):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
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)}'"
|
||||
cursor.execute(sql)
|
||||
res = self.discorddbconn.get_db().commit()
|
||||
else:
|
||||
raise QuestionNotFound()
|
||||
|
||||
def get_questions(self):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"SELECT * FROM {self.tablename}"
|
||||
cursor.execute(sql)
|
||||
return list(cursor.fetchall())
|
||||
|
||||
def count_questions(self):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {}".format(self.tablename))
|
||||
res = cursor.fetchone()
|
||||
return res[0]
|
||||
|
||||
def get_random_question(self):
|
||||
if self.count_questions != 0:
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = f"SELECT * FROM {self.tablename} ORDER BY RAND() LIMIT 1"
|
||||
cursor.execute(sql)
|
||||
return list(cursor.fetchone())
|
||||
else:
|
||||
raise QuestionNotFound()
|
||||
|
||||
def close(self):
|
||||
self.discorddbconn.close()
|
||||
|
||||
|
||||
|
||||
# this will save the uuid of the player and discord id
|
||||
class PlayerDBLinker:
|
||||
def __init__(self):
|
||||
|
||||
@@ -20,3 +20,4 @@ modPlusRoles = [roleAdmin, roleMod, roleOwner]
|
||||
|
||||
# Channels
|
||||
ModLogs = 760807882899193867
|
||||
QuizChannelID = 774418250665951232
|
||||
|
||||
Reference in New Issue
Block a user