Doing a trivia

This commit is contained in:
NikolajDanger
2020-04-06 14:37:10 +02:00
parent 2a27ef61de
commit a0451d5dbe
5 changed files with 128 additions and 118 deletions

View File

@ -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:

View File

@ -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

View File

@ -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)
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
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()
# 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()
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])}
# 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()
return "\n".join(['%s: %s' % (key, value) for (key, value) in items.items()])
else:
return "I didn't quite understand that"
# 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")
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
finally:
f.close()

View File

@ -1,2 +1,3 @@
from .generators import nameGen, tavernGen
from .movie import movieFunc
from .trivia import triviaCountPoints, triviaStart, triviaOtherThing

81
funcs/other/trivia.py Normal file
View File

@ -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