Trivia command
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -149,4 +149,5 @@ static
|
|||||||
.vscode/
|
.vscode/
|
||||||
token.txt
|
token.txt
|
||||||
resources/swcharacters.json
|
resources/swcharacters.json
|
||||||
|
resources/trivia.json
|
||||||
resources/destinyPoints.txt
|
resources/destinyPoints.txt
|
||||||
|
56
Gwendolyn.py
56
Gwendolyn.py
@ -20,6 +20,17 @@ except:
|
|||||||
finally:
|
finally:
|
||||||
f.close()
|
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
|
# Creates destinyPoints.txt if it doesn't exist
|
||||||
try:
|
try:
|
||||||
f = open("resources/destinyPoints.txt","r")
|
f = open("resources/destinyPoints.txt","r")
|
||||||
@ -34,7 +45,7 @@ try:
|
|||||||
f = open("token.txt","r")
|
f = open("token.txt","r")
|
||||||
except:
|
except:
|
||||||
funcs.logThis("token.txt didn't exist. Write your bot token in the file.")
|
funcs.logThis("token.txt didn't exist. Write your bot token in the file.")
|
||||||
with open("resources/destinyPoints.txt","w") as f:
|
with open("resources/token.txt","w") as f:
|
||||||
f.write("Replace this line with bot token")
|
f.write("Replace this line with bot token")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
@ -69,6 +80,15 @@ async def on_message(message):
|
|||||||
if message.author.name == "Nikolaj":
|
if message.author.name == "Nikolaj":
|
||||||
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
|
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
|
||||||
await message.channel.send("Logging out...")
|
await message.channel.send("Logging out...")
|
||||||
|
|
||||||
|
with open("resources/trivia.json","r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
data["questions"] = {}
|
||||||
|
|
||||||
|
with open("resources/trivia.json","w") as f:
|
||||||
|
json.dump(data,f,indent=4)
|
||||||
|
|
||||||
await client.logout()
|
await client.logout()
|
||||||
else:
|
else:
|
||||||
funcs.logThis(message.author.name+" tried to run "+message.content)
|
funcs.logThis(message.author.name+" tried to run "+message.content)
|
||||||
@ -225,6 +245,40 @@ async def on_message(message):
|
|||||||
await message.channel.send(embed = embed)
|
await message.channel.send(embed = embed)
|
||||||
else:
|
else:
|
||||||
await message.channel.send(content)
|
await message.channel.send(content)
|
||||||
|
|
||||||
|
elif message.content.lower().startswith("!trivia"):
|
||||||
|
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
|
||||||
|
if message.content.lower() == "!trivia" or message.content.lower() == "!trivia ":
|
||||||
|
question, answers, correctAnswer = funcs.triviaStart(str(message.channel))
|
||||||
|
if answers != "":
|
||||||
|
results = "**"+question+"**\n"
|
||||||
|
for answer in range(len(answers)):
|
||||||
|
results += chr(answer+97) + ") "+answers[answer]+"\n"
|
||||||
|
|
||||||
|
await message.channel.send(results)
|
||||||
|
|
||||||
|
await asyncio.sleep(60)
|
||||||
|
|
||||||
|
funcs.triviaCountPoints(str(message.channel))
|
||||||
|
|
||||||
|
with open("resources/trivia.json", "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
del data["questions"][str(message.channel)]
|
||||||
|
with open("resources/trivia.json", "w") as f:
|
||||||
|
json.dump(data,f,indent=4)
|
||||||
|
|
||||||
|
funcs.logThis("Time's up for the trivia question in "+str(message.channel))
|
||||||
|
await message.channel.send("Time's up! The answer was \""+chr(correctAnswer)+") "+answers[correctAnswer-97]+"\". Anyone who answered that has gotten a point")
|
||||||
|
else:
|
||||||
|
await message.channel.send(question)
|
||||||
|
|
||||||
|
elif message.content.lower().startswith("!trivia "):
|
||||||
|
command = message.content.lower().replace("!trivia ","")
|
||||||
|
await message.channel.send(funcs.triviaOtherThing(message.author.name,str(message.channel),command))
|
||||||
|
else:
|
||||||
|
funcs.logThis("I didn't understand that")
|
||||||
|
await message.channel.send("I didn't understand that")
|
||||||
|
|
||||||
|
|
||||||
# Runs the whole shabang
|
# Runs the whole shabang
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
from .gwendolynFuncs import helloFunc, roll_dice, cap, imageFunc, logThis, findWikiPage
|
from .gwendolynFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, triviaStart, triviaOtherThing, triviaCountPoints
|
||||||
|
|
||||||
from .swfuncs import parseChar, parseRoll, parseDestiny
|
from .swfuncs import parseChar, parseRoll, parseDestiny
|
||||||
|
|
||||||
from .lookup import spellFunc, monsterFunc
|
from .lookup import spellFunc, monsterFunc
|
||||||
|
|
||||||
from .other import nameGen, tavernGen, movieFunc
|
from .other import nameGen, tavernGen, movieFunc
|
||||||
|
|
||||||
|
from .roll import roll_dice
|
@ -3,7 +3,7 @@ import re # Used by roll_dice
|
|||||||
import datetime # Used by helloFunc
|
import datetime # Used by helloFunc
|
||||||
import json # Used by spellFunc
|
import json # Used by spellFunc
|
||||||
import random # Used by imageFunc
|
import random # Used by imageFunc
|
||||||
import urllib # Used by imageFunc
|
import urllib # Used by imageFunc and triviaStart
|
||||||
import imdb # Used by movieFunc
|
import imdb # Used by movieFunc
|
||||||
import time # Used for logging
|
import time # Used for logging
|
||||||
import logging # Used for... you know... logging
|
import logging # Used for... you know... logging
|
||||||
@ -13,26 +13,6 @@ from .roll import dice
|
|||||||
|
|
||||||
logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
|
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
|
# Capitalizes all words except some of them
|
||||||
no_caps_list = ["of","the"]
|
no_caps_list = ["of","the"]
|
||||||
def cap(s):
|
def cap(s):
|
||||||
@ -143,4 +123,80 @@ def findWikiPage(search : str):
|
|||||||
return "", "Sorry. Fucked that one up", ""
|
return "", "Sorry. Fucked that one up", ""
|
||||||
else:
|
else:
|
||||||
logThis("Couldn't find the page")
|
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
|
||||||
|
1
funcs/roll/__init__.py
Normal file
1
funcs/roll/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .dice import roll_dice
|
@ -18,6 +18,23 @@ DICE_PATTERN = re.compile(
|
|||||||
IGNORECASE)
|
IGNORECASE)
|
||||||
|
|
||||||
|
|
||||||
|
def roll_dice(author : str, rollStr : str = "1d20"):
|
||||||
|
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 = 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
|
||||||
|
return(outputs)
|
||||||
|
|
||||||
def list_get(index, default, l):
|
def list_get(index, default, l):
|
||||||
try:
|
try:
|
||||||
a = l[index]
|
a = l[index]
|
||||||
|
@ -40,7 +40,12 @@ class testGwendolynFuncs(unittest.TestCase):
|
|||||||
title, content, thumbnail = funcs.findWikiPage("Moldaw")
|
title, content, thumbnail = funcs.findWikiPage("Moldaw")
|
||||||
self.assertEqual(title,"Moldaw Dragniel")
|
self.assertEqual(title,"Moldaw Dragniel")
|
||||||
self.assertEqual(content,"Moldaw Dragniel (Født: Moldaw Geisler Dragniel) er en Rock Gnome fra den sydvestlige del af Zules Kongeriget i kejserriget Crozea. Han kommer fra den store landsby Ginti, hvor hans forældre arbejdede som håndværkere.")
|
self.assertEqual(content,"Moldaw Dragniel (Født: Moldaw Geisler Dragniel) er en Rock Gnome fra den sydvestlige del af Zules Kongeriget i kejserriget Crozea. Han kommer fra den store landsby Ginti, hvor hans forældre arbejdede som håndværkere.")
|
||||||
self.assertEqual(thumbnail,"https://vignette.wikia.nocookie.net/senkulpa/images/9/9e/Moldaw.png")
|
self.assertEqual(thumbnail,"https://vignette.wikia.nocookie.net/senkulpa/images/9/9e/Moldaw.png/revision/latest?path-prefix=da")
|
||||||
|
|
||||||
|
def testTriviaStart(self):
|
||||||
|
info = funcs.triviaStart()
|
||||||
|
print(info)
|
||||||
|
self.assertNotEqual(info,"")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
Reference in New Issue
Block a user