Trivia command

This commit is contained in:
NikolajDanger
2020-04-05 18:37:59 +02:00
parent 906ff4361a
commit 1e2f64c67c
7 changed files with 162 additions and 26 deletions

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
import urllib # Used by imageFunc and triviaStart
import imdb # Used by movieFunc
import time # Used for logging
import logging # Used for... you know... logging
@ -13,26 +13,6 @@ from .roll import dice
logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
# I stole this. It rolls dice. I am not qualified to comment it
def roll_dice(author : str, rollStr : str = "1d20"):
logThis("Rolling "+str(rollStr))
if rollStr == '0/0': # easter eggs
return("What do you expect me to do, destroy the universe?")
adv = 0
if re.search('(^|\s+)(adv|dis)(\s+|$)', rollStr) is not None:
adv = 1 if re.search('(^|\s+)adv(\s+|$)', rollStr) is not None else -1
rollStr = re.sub('(adv|dis)(\s+|$)', '', rollStr)
res = dice.roll(rollStr, adv=adv)
out = res.result
outStr = author + ' :game_die:\n' + out
if len(outStr) > 1999:
outputs = author + ' :game_die:\n[Output truncated due to length]\n**Result:** ' + str(res.plain)
else:
outputs = outStr
logThis("Successfully ran !roll")
return(outputs)
# Capitalizes all words except some of them
no_caps_list = ["of","the"]
def cap(s):
@ -143,4 +123,80 @@ def findWikiPage(search : str):
return "", "Sorry. Fucked that one up", ""
else:
logThis("Couldn't find the page")
return "", "Couldn't find 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
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