mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-07 03:38:53 -05:00
Added !triviaset
This commit is contained in:
parent
0b0931ba87
commit
1ef2ed4c04
@ -2,18 +2,61 @@ import discord
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from random import randint
|
from random import randint
|
||||||
from random import choice as randchoice
|
from random import choice as randchoice
|
||||||
|
from .utils.dataIO import fileIO
|
||||||
|
from .utils import checks
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
settings = {"TRIVIA_MAX_SCORE" : 10, "TRIVIA_TIMEOUT" : 120, "TRIVIA_DELAY" : 15, "TRIVIA_BOT_PLAYS" : False}
|
|
||||||
|
|
||||||
class Trivia:
|
class Trivia:
|
||||||
"""General commands."""
|
"""General commands."""
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.trivia_sessions = []
|
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)
|
@commands.command(pass_context=True)
|
||||||
async def trivia(self, ctx, list_name : str=None):
|
async def trivia(self, ctx, list_name : str=None):
|
||||||
@ -33,7 +76,7 @@ class Trivia:
|
|||||||
else:
|
else:
|
||||||
await self.bot.say("There's no trivia session ongoing in this channel.")
|
await self.bot.say("There's no trivia session ongoing in this channel.")
|
||||||
elif not await get_trivia_by_channel(message.channel):
|
elif not await get_trivia_by_channel(message.channel):
|
||||||
t = TriviaSession(message)
|
t = TriviaSession(message, self.settings)
|
||||||
self.trivia_sessions.append(t)
|
self.trivia_sessions.append(t)
|
||||||
await t.load_questions(message.content)
|
await t.load_questions(message.content)
|
||||||
else:
|
else:
|
||||||
@ -62,7 +105,7 @@ class Trivia:
|
|||||||
await self.bot.say("There are no trivia lists available.")
|
await self.bot.say("There are no trivia lists available.")
|
||||||
|
|
||||||
class TriviaSession():
|
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.gave_answer = ["I know this one! {}!", "Easy: {}.", "Oh really? It's {} of course."]
|
||||||
self.current_q = None # {"QUESTION" : "String", "ANSWERS" : []}
|
self.current_q = None # {"QUESTION" : "String", "ANSWERS" : []}
|
||||||
self.question_list = ""
|
self.question_list = ""
|
||||||
@ -71,6 +114,7 @@ class TriviaSession():
|
|||||||
self.status = None
|
self.status = None
|
||||||
self.timer = None
|
self.timer = None
|
||||||
self.count = 0
|
self.count = 0
|
||||||
|
self.settings = settings
|
||||||
|
|
||||||
async def load_questions(self, msg):
|
async def load_questions(self, msg):
|
||||||
msg = msg.split(" ")
|
msg = msg.split(" ")
|
||||||
@ -127,7 +171,7 @@ class TriviaSession():
|
|||||||
|
|
||||||
async def new_question(self):
|
async def new_question(self):
|
||||||
for score in self.score_list.values():
|
for score in self.score_list.values():
|
||||||
if score == settings["TRIVIA_MAX_SCORE"]:
|
if score == self.settings["TRIVIA_MAX_SCORE"]:
|
||||||
await self.end_game()
|
await self.end_game()
|
||||||
return True
|
return True
|
||||||
if self.question_list == []:
|
if self.question_list == []:
|
||||||
@ -139,8 +183,8 @@ class TriviaSession():
|
|||||||
self.count += 1
|
self.count += 1
|
||||||
self.timer = int(time.perf_counter())
|
self.timer = int(time.perf_counter())
|
||||||
await trivia_manager.bot.say("**Question number {}!**\n\n{}".format(str(self.count), self.current_q["QUESTION"]))
|
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"]:
|
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())) >= settings["TRIVIA_TIMEOUT"]:
|
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 trivia_manager.bot.say("Guys...? Well, I guess I'll stop then.")
|
||||||
await self.stop_trivia()
|
await self.stop_trivia()
|
||||||
return True
|
return True
|
||||||
@ -154,9 +198,9 @@ class TriviaSession():
|
|||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
msg = randchoice(self.gave_answer).format(self.current_q["ANSWERS"][0])
|
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!"
|
msg += " **+1** for me!"
|
||||||
self.add_point(self.bot.user.name)
|
self.add_point(trivia_manager.bot.user.name)
|
||||||
self.current_q["ANSWERS"] = []
|
self.current_q["ANSWERS"] = []
|
||||||
await trivia_manager.bot.say(msg)
|
await trivia_manager.bot.say(msg)
|
||||||
await trivia_manager.bot.send_typing(self.channel)
|
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)
|
trvsession = await get_trivia_by_channel(message.channel)
|
||||||
await trvsession.check_answer(message)
|
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):
|
def setup(bot):
|
||||||
global trivia_manager
|
global trivia_manager
|
||||||
|
check_folders()
|
||||||
|
check_files()
|
||||||
bot.add_listener(check_messages, "on_message")
|
bot.add_listener(check_messages, "on_message")
|
||||||
trivia_manager = Trivia(bot)
|
trivia_manager = Trivia(bot)
|
||||||
bot.add_cog(trivia_manager)
|
bot.add_cog(trivia_manager)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user