diff --git a/Gwendolyn.py b/Gwendolyn.py index 2ba9363..a85865a 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -9,47 +9,7 @@ import json import funcs -# Creates swcharacters.json if it doesn't exist -try: - f = open("resources/swcharacters.json","r") -except: - funcs.logThis("swcharacters.json didn't exist. Making it now.") - emptyDict = {} - with open("resources/swcharacters.json","w") as f: - json.dump(emptyDict,f,indent = 4) -finally: - f.close() - -# Creates trivia.json if it doesn't exist -try: - f = open("resources/trivia.json","r") -except: - funcs.logThis("trivia.json didn't exist. Making it now.") - data = {"questions":{},"users":{}} - with open("resources/trivia.json","w") as f: - json.dump(data,f,indent = 4) -finally: - f.close() - -# Creates destinyPoints.txt if it doesn't exist -try: - f = open("resources/destinyPoints.txt","r") -except: - funcs.logThis("destinyPoints.txt didn't exist. Making it now.") - with open("resources/destinyPoints.txt","w") as f: - f.write("") -finally: - f.close() -# Creates token.txt -try: - f = open("token.txt","r") -except: - funcs.logThis("token.txt didn't exist. Write your bot token in the file.") - with open("resources/token.txt","w") as f: - f.write("Replace this line with bot token") - -finally: - f.close() +funcs.makeFiles() # Gets secret bot token with open("token.txt","r") as f: diff --git a/funcs/__init__.py b/funcs/__init__.py index cc793cb..e12dd3c 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -1,9 +1,9 @@ -from .gwendolynFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, triviaStart, triviaOtherThing, triviaCountPoints +from .gwendolynFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles from .swfuncs import parseChar, parseRoll, parseDestiny from .lookup import spellFunc, monsterFunc -from .other import nameGen, tavernGen, movieFunc +from .other import nameGen, tavernGen, movieFunc, triviaStart, triviaOtherThing, triviaCountPoints from .roll import roll_dice \ No newline at end of file diff --git a/funcs/gwendolynFuncs.py b/funcs/gwendolynFuncs.py index dddd6c8..d945d50 100644 --- a/funcs/gwendolynFuncs.py +++ b/funcs/gwendolynFuncs.py @@ -3,7 +3,7 @@ import re # Used by roll_dice import datetime # Used by helloFunc import json # Used by spellFunc import random # Used by imageFunc -import urllib # Used by imageFunc and triviaStart +import urllib # Used by imageFunc import imdb # Used by movieFunc import time # Used for logging import logging # Used for... you know... logging @@ -125,78 +125,46 @@ def findWikiPage(search : str): logThis("Couldn't find the page") return "", "Couldn't find page", "" -def triviaStart(channel : str): - with open("resources/trivia.json", "r") as f: - triviaFile = json.load(f) - - logThis("Trying to find a trivia question for "+channel) +def makeFiles(): + # Creates swcharacters.json if it doesn't exist + try: + f = open("resources/swcharacters.json","r") + except: + logThis("swcharacters.json didn't exist. Making it now.") + emptyDict = {} + with open("resources/swcharacters.json","w") as f: + json.dump(emptyDict,f,indent = 4) + finally: + f.close() - if channel not in triviaFile["questions"]: - with urllib.request.urlopen("https://opentdb.com/api.php?amount=10&type=multiple") as response: - data = json.loads(response.read()) + # Creates trivia.json if it doesn't exist + try: + f = open("resources/trivia.json","r") + except: + logThis("trivia.json didn't exist. Making it now.") + data = {"questions":{},"users":{}} + with open("resources/trivia.json","w") as f: + json.dump(data,f,indent = 4) + finally: + f.close() + + # Creates destinyPoints.txt if it doesn't exist + try: + f = open("resources/destinyPoints.txt","r") + except: + logThis("destinyPoints.txt didn't exist. Making it now.") + with open("resources/destinyPoints.txt","w") as f: + f.write("") + finally: + f.close() - 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["questions"][channel] = {"answer" : str(chr(correctAnswer)),"players" : {}} - with open("resources/trivia.json", "w") as f: - json.dump(triviaFile,f,indent=4) - - question = data["results"][0]["question"].replace("'","\'").replace(""","\"") - - return question, answers, correctAnswer - else: - logThis("There was already a trivia question for that channel") - return "There's already a trivia question going on. Try again in like, a minute", "", "" - -def triviaOtherThing(user : str, channel : str, command : str): - with open("resources/trivia.json", "r") as f: - data = json.load(f) - - if command in ["a","b","c","d"]: - if channel in data["questions"]: - if user not in data["questions"][channel]["players"]: - logThis(user+" answered the question in "+channel) - - data["questions"][channel]["players"][user] = command - - with open("resources/trivia.json", "w") as f: - json.dump(data,f,indent=4) - - return "Locked in "+user+"'s answer" - else: - return user+" has already answered this question" - else: - return "There's no question right now" - elif command == "p": - items = {k: v for k, v in sorted(data["users"].items(), key=lambda item: item[1])} - - return "\n".join(['%s: %s' % (key, value) for (key, value) in items.items()]) - else: - return "I didn't quite understand that" - -def triviaCountPoints(channel : str): - with open("resources/trivia.json", "r") as f: - data = json.load(f) - - logThis("Counting points for question in "+channel) - - if channel in data["questions"]: - for player, answer in data["questions"][channel]["players"].items(): - if answer == data["questions"][channel]["answer"]: - if player in data["users"]: - points = data["users"][player] - data["users"][player] = points + 1 - else: - data["users"][player] = 1 - - with open("resources/trivia.json", "w") as f: - json.dump(data,f,indent=4) - - else: - logThis("Couldn't find the question") - - return None + # Creates token.txt if it doesn't exist + try: + f = open("token.txt","r") + except: + logThis("token.txt didn't exist. Write your bot token in the file.") + with open("resources/token.txt","w") as f: + f.write("Replace this line with bot token") + + finally: + f.close() \ No newline at end of file diff --git a/funcs/other/__init__.py b/funcs/other/__init__.py index 8f788c5..bc40ec9 100644 --- a/funcs/other/__init__.py +++ b/funcs/other/__init__.py @@ -1,2 +1,3 @@ from .generators import nameGen, tavernGen -from .movie import movieFunc \ No newline at end of file +from .movie import movieFunc +from .trivia import triviaCountPoints, triviaStart, triviaOtherThing \ No newline at end of file diff --git a/funcs/other/trivia.py b/funcs/other/trivia.py new file mode 100644 index 0000000..6dd86a8 --- /dev/null +++ b/funcs/other/trivia.py @@ -0,0 +1,81 @@ +import json +import urllib +import random + +from funcs import logThis + +def triviaStart(channel : str): + with open("resources/trivia.json", "r") as f: + triviaFile = json.load(f) + + logThis("Trying to find a trivia question for "+channel) + + if channel not in triviaFile["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["questions"][channel] = {"answer" : str(chr(correctAnswer)),"players" : {}} + with open("resources/trivia.json", "w") as f: + json.dump(triviaFile,f,indent=4) + + question = data["results"][0]["question"].replace("'","\'").replace(""","\"") + + return question, answers, correctAnswer + else: + logThis("There was already a trivia question for that channel") + return "There's already a trivia question going on. Try again in like, a minute", "", "" + +def triviaOtherThing(user : str, channel : str, command : str): + with open("resources/trivia.json", "r") as f: + data = json.load(f) + + if command in ["a","b","c","d"]: + if channel in data["questions"]: + if user not in data["questions"][channel]["players"]: + logThis(user+" answered the question in "+channel) + + data["questions"][channel]["players"][user] = command + + with open("resources/trivia.json", "w") as f: + json.dump(data,f,indent=4) + + return "Locked in "+user+"'s answer" + else: + return user+" has already answered this question" + else: + return "There's no question right now" + elif command == "p": + items = {k: v for k, v in sorted(data["users"].items(), key=lambda item: item[1])} + + return "\n".join(['%s: %s' % (key, value) for (key, value) in items.items()]) + else: + return "I didn't quite understand that" + +def triviaCountPoints(channel : str): + with open("resources/trivia.json", "r") as f: + data = json.load(f) + + logThis("Counting points for question in "+channel) + + if channel in data["questions"]: + for player, answer in data["questions"][channel]["players"].items(): + if answer == data["questions"][channel]["answer"]: + if player in data["users"]: + points = data["users"][player] + data["users"][player] = points + 1 + else: + data["users"][player] = 1 + + with open("resources/trivia.json", "w") as f: + json.dump(data,f,indent=4) + + else: + logThis("Couldn't find the question") + + return None \ No newline at end of file