From 1ef2ed4c044ba45c875da42089e67f723a8c7c07 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Sat, 9 Apr 2016 21:03:33 +0200 Subject: [PATCH] Added !triviaset --- cogs/trivia.py | 78 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/cogs/trivia.py b/cogs/trivia.py index d24fa47e5..3a0b46a8c 100644 --- a/cogs/trivia.py +++ b/cogs/trivia.py @@ -2,18 +2,61 @@ import discord from discord.ext import commands from random import randint from random import choice as randchoice +from .utils.dataIO import fileIO +from .utils import checks import datetime import time import os import asyncio -settings = {"TRIVIA_MAX_SCORE" : 10, "TRIVIA_TIMEOUT" : 120, "TRIVIA_DELAY" : 15, "TRIVIA_BOT_PLAYS" : False} - class Trivia: """General commands.""" def __init__(self, bot): self.bot = bot self.trivia_sessions = [] + self.settings = fileIO("data/trivia/settings.json", "load") + + @commands.group(pass_context=True) + @checks.mod_or_permissions(manage_roles=True) + async def triviaset(self, ctx): + """Change trivia settings""" + if ctx.invoked_subcommand is None: + msg = "```\n" + for k, v in self.settings.items(): + msg += "{}: {}\n".format(k, v) + msg += "```\nSee {}help triviaset to edit the settings".format(ctx.prefix) + await self.bot.say(msg) + + @triviaset.command() + async def maxscore(self, score : int): + """Points required to win""" + if score > 0: + self.settings["TRIVIA_MAX_SCORE"] = score + fileIO("data/trivia/settings.json", "save", self.settings) + await self.bot.say("Points required to win set to {}".format(str(score))) + else: + await self.bot.say("Score must be superior to 0.") + + @triviaset.command() + async def timelimit(self, seconds : int): + """Maximum seconds to answer""" + if seconds > 4: + self.settings["TRIVIA_DELAY"] = seconds + fileIO("data/trivia/settings.json", "save", self.settings) + await self.bot.say("Maximum seconds to answer set to {}".format(str(seconds))) + else: + await self.bot.say("Seconds must be at least 5.") + + @triviaset.command() + async def botplays(self): + """Red gains points""" + if self.settings["TRIVIA_BOT_PLAYS"] is True: + self.settings["TRIVIA_BOT_PLAYS"] = False + await self.bot.say("Alright, I won't embarass you at trivia anymore.") + else: + self.settings["TRIVIA_BOT_PLAYS"] = True + await self.bot.say("I'll gain a point everytime you don't answer in time.") + fileIO("data/trivia/settings.json", "save", self.settings) @commands.command(pass_context=True) async def trivia(self, ctx, list_name : str=None): @@ -33,7 +76,7 @@ class Trivia: else: await self.bot.say("There's no trivia session ongoing in this channel.") elif not await get_trivia_by_channel(message.channel): - t = TriviaSession(message) + t = TriviaSession(message, self.settings) self.trivia_sessions.append(t) await t.load_questions(message.content) else: @@ -62,7 +105,7 @@ class Trivia: await self.bot.say("There are no trivia lists available.") class TriviaSession(): - def __init__(self, message): + def __init__(self, message, settings): self.gave_answer = ["I know this one! {}!", "Easy: {}.", "Oh really? It's {} of course."] self.current_q = None # {"QUESTION" : "String", "ANSWERS" : []} self.question_list = "" @@ -71,6 +114,7 @@ class TriviaSession(): self.status = None self.timer = None self.count = 0 + self.settings = settings async def load_questions(self, msg): msg = msg.split(" ") @@ -127,7 +171,7 @@ class TriviaSession(): async def new_question(self): for score in self.score_list.values(): - if score == settings["TRIVIA_MAX_SCORE"]: + if score == self.settings["TRIVIA_MAX_SCORE"]: await self.end_game() return True if self.question_list == []: @@ -139,8 +183,8 @@ class TriviaSession(): self.count += 1 self.timer = int(time.perf_counter()) await trivia_manager.bot.say("**Question number {}!**\n\n{}".format(str(self.count), self.current_q["QUESTION"])) - while self.status != "correct answer" and abs(self.timer - int(time.perf_counter())) <= settings["TRIVIA_DELAY"]: - if abs(self.timeout - int(time.perf_counter())) >= settings["TRIVIA_TIMEOUT"]: + while self.status != "correct answer" and abs(self.timer - int(time.perf_counter())) <= self.settings["TRIVIA_DELAY"]: + if abs(self.timeout - int(time.perf_counter())) >= self.settings["TRIVIA_TIMEOUT"]: await trivia_manager.bot.say("Guys...? Well, I guess I'll stop then.") await self.stop_trivia() return True @@ -154,9 +198,9 @@ class TriviaSession(): return True else: msg = randchoice(self.gave_answer).format(self.current_q["ANSWERS"][0]) - if settings["TRIVIA_BOT_PLAYS"]: + if self.settings["TRIVIA_BOT_PLAYS"]: msg += " **+1** for me!" - self.add_point(self.bot.user.name) + self.add_point(trivia_manager.bot.user.name) self.current_q["ANSWERS"] = [] await trivia_manager.bot.say(msg) await trivia_manager.bot.send_typing(self.channel) @@ -210,8 +254,24 @@ async def check_messages(message): trvsession = await get_trivia_by_channel(message.channel) await trvsession.check_answer(message) +def check_folders(): + folders = ("data", "data/trivia/") + for folder in folders: + if not os.path.exists(folder): + print("Creating " + folder + " folder...") + os.makedirs(folder) + +def check_files(): + settings = {"TRIVIA_MAX_SCORE" : 10, "TRIVIA_TIMEOUT" : 120, "TRIVIA_DELAY" : 15, "TRIVIA_BOT_PLAYS" : False} + + if not os.path.isfile("data/trivia/settings.json"): + print("Creating empty settings.json...") + fileIO("data/trivia/settings.json", "save", settings) + def setup(bot): global trivia_manager + check_folders() + check_files() bot.add_listener(check_messages, "on_message") trivia_manager = Trivia(bot) bot.add_cog(trivia_manager)