:spakles: Database and OOP
This commit is contained in:
@ -5,88 +5,84 @@ import random
|
||||
from . import money
|
||||
from funcs import logThis
|
||||
|
||||
# Starts a game of trivia. Downloads a question with answers, shuffles the wrong answers with the
|
||||
# correct answer and returns the questions and answers. Also saves the question in the games.json file.
|
||||
def triviaStart(channel : str):
|
||||
with open("resources/games/games.json", "r") as f:
|
||||
triviaFile = json.load(f)
|
||||
|
||||
logThis("Trying to find a trivia question for "+channel)
|
||||
class Trivia():
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
if channel not in triviaFile["trivia questions"]:
|
||||
with urllib.request.urlopen("https://opentdb.com/api.php?amount=10&type=multiple") as response:
|
||||
data = json.loads(response.read())
|
||||
|
||||
logThis("Found the question \""+data["results"][0]["question"]+"\"")
|
||||
answers = data["results"][0]["incorrect_answers"]
|
||||
answers.append(data["results"][0]["correct_answer"])
|
||||
random.shuffle(answers)
|
||||
correctAnswer = answers.index(data["results"][0]["correct_answer"]) + 97
|
||||
|
||||
triviaFile["trivia questions"][channel] = {"answer" : str(chr(correctAnswer)),"players" : {}}
|
||||
with open("resources/games/games.json", "w") as f:
|
||||
json.dump(triviaFile,f,indent=4)
|
||||
# Starts a game of trivia. Downloads a question with answers, shuffles the wrong answers with the
|
||||
# correct answer and returns the questions and answers. Also saves the question in the games.json file.
|
||||
def triviaStart(self, channel : str):
|
||||
question = self.bot.database["trivia questions"].find_one({"_id":channel})
|
||||
|
||||
replacements = {"'": "\'",
|
||||
""": "\"",
|
||||
"“": "\"",
|
||||
"”": "\"",
|
||||
"é": "é"}
|
||||
question = data["results"][0]["question"]
|
||||
logThis("Trying to find a trivia question for "+channel)
|
||||
|
||||
if question == None:
|
||||
with urllib.request.urlopen("https://opentdb.com/api.php?amount=10&type=multiple") as response:
|
||||
data = json.loads(response.read())
|
||||
|
||||
logThis("Found the question \""+data["results"][0]["question"]+"\"")
|
||||
answers = data["results"][0]["incorrect_answers"]
|
||||
answers.append(data["results"][0]["correct_answer"])
|
||||
random.shuffle(answers)
|
||||
correctAnswer = answers.index(data["results"][0]["correct_answer"]) + 97
|
||||
|
||||
self.bot.database["trivia questions"].insert_one({"_id":channel,"answer" : str(chr(correctAnswer)),"players" : {}})
|
||||
|
||||
replacements = {"'": "\'",
|
||||
""": "\"",
|
||||
"“": "\"",
|
||||
"”": "\"",
|
||||
"é": "é"}
|
||||
question = data["results"][0]["question"]
|
||||
|
||||
for key, value in replacements.items():
|
||||
question = question.replace(key,value)
|
||||
|
||||
for answer in answers:
|
||||
for key, value in replacements.items():
|
||||
answer = answer.replace(key,value)
|
||||
question = question.replace(key,value)
|
||||
|
||||
return question, answers, correctAnswer
|
||||
else:
|
||||
logThis("There was already a trivia question for that channel (error code 1106)")
|
||||
return "There's already a trivia question going on. Try again in like, a minute (error code 1106)", "", ""
|
||||
for answer in answers:
|
||||
for key, value in replacements.items():
|
||||
answer = answer.replace(key,value)
|
||||
|
||||
# Lets players answer a trivia question
|
||||
def triviaAnswer(user : str, channel : str, command : str):
|
||||
with open("resources/games/games.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
if command in ["a","b","c","d"]:
|
||||
if channel in data["trivia questions"]:
|
||||
if user not in data["trivia questions"][channel]["players"]:
|
||||
logThis(user+" answered the question in "+channel)
|
||||
|
||||
data["trivia questions"][channel]["players"][user] = command
|
||||
|
||||
with open("resources/games/games.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
return "Locked in "+user+"'s answer"
|
||||
else:
|
||||
logThis(user+" has already answered this question (error code 1105)")
|
||||
return user+" has already answered this question (error code 1105)"
|
||||
return question, answers, correctAnswer
|
||||
else:
|
||||
logThis("There's no question right now (error code 1104)")
|
||||
return "There's no question right now (error code 1104)"
|
||||
else:
|
||||
logThis("I didn't quite understand that (error code 1103)")
|
||||
return "I didn't quite understand that (error code 1103)"
|
||||
logThis("There was already a trivia question for that channel (error code 1106)")
|
||||
return "There's already a trivia question going on. Try again in like, a minute (error code 1106)", "", ""
|
||||
|
||||
# Lets players answer a trivia question
|
||||
def triviaAnswer(self, user : str, channel : str, command : str):
|
||||
question = self.bot.database["trivia questions"].find_one({"_id":channel})
|
||||
|
||||
if command in ["a","b","c","d"]:
|
||||
if question != None:
|
||||
if user not in question["players"]:
|
||||
logThis(user+" answered the question in "+channel)
|
||||
|
||||
self.bot.database["trivia questions"].update_one({"_id":channel},{"$set":{"players."+user : command}})
|
||||
|
||||
return "Locked in "+user+"'s answer"
|
||||
else:
|
||||
logThis(user+" has already answered this question (error code 1105)")
|
||||
return user+" has already answered this question (error code 1105)"
|
||||
else:
|
||||
logThis("There's no question right now (error code 1104)")
|
||||
return "There's no question right now (error code 1104)"
|
||||
else:
|
||||
logThis("I didn't quite understand that (error code 1103)")
|
||||
return "I didn't quite understand that (error code 1103)"
|
||||
|
||||
|
||||
# Adds 1 GwendoBuck to each player that got the question right and deletes question from games.json.
|
||||
def triviaCountPoints(channel : str):
|
||||
with open("resources/games/games.json", "r") as f:
|
||||
data = json.load(f)
|
||||
# Adds 1 GwendoBuck to each player that got the question right and deletes question from games.json.
|
||||
def triviaCountPoints(self, channel : str):
|
||||
question = self.bot.database["trivia questions"].find_one({"_id":channel})
|
||||
|
||||
logThis("Counting points for question in "+channel)
|
||||
logThis("Counting points for question in "+channel)
|
||||
|
||||
if channel in data["trivia questions"]:
|
||||
for player, answer in data["trivia questions"][channel]["players"].items():
|
||||
if answer == data["trivia questions"][channel]["answer"]:
|
||||
money.addMoney(player,1)
|
||||
|
||||
if question != None:
|
||||
for player, answer in question["players"].items():
|
||||
if answer == question["answer"]:
|
||||
self.bot.money.addMoney(player,1)
|
||||
|
||||
else:
|
||||
logThis("Couldn't find the question (error code 1102)")
|
||||
|
||||
return None
|
||||
else:
|
||||
logThis("Couldn't find the question (error code 1102)")
|
||||
|
||||
return None
|
||||
|
Reference in New Issue
Block a user