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

@ -20,6 +20,17 @@ except:
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")
@ -34,7 +45,7 @@ try:
f = open("token.txt","r")
except:
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")
finally:
@ -69,6 +80,15 @@ async def on_message(message):
if message.author.name == "Nikolaj":
funcs.logThis(message.author.name+" ran \""+message.content+"\"")
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()
else:
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)
else:
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