Browse Source

trivia now has more cli like argument parsing.

tags/v2.0.0
Roxie Gibson 5 years ago
parent
commit
938aaa468d
1 changed files with 33 additions and 30 deletions
  1. +33
    -30
      roxbot/cogs/trivia.py

+ 33
- 30
roxbot/cogs/trivia.py View File

@@ -33,7 +33,7 @@ from random import shuffle
from collections import OrderedDict
from discord.ext import commands

from roxbot import http, checks, EmbedColours
import roxbot


# TODO: Refactor the game into its own class and have the commands interact with that api.
@@ -49,6 +49,7 @@ class Trivia:
def __init__(self, bot_client):
# Get emoji objects here for the reactions. Basically to speedup the reactions for the game.
self.bot = bot_client
self.lengths = {"short": 5, "medium": 10, "long": 15}
a_emoji = self.bot.get_emoji(419572828854026252) or "🇦"
b_emoji = self.bot.get_emoji(419572828925329429) or "🇧"
c_emoji = self.bot.get_emoji(419572829231775755) or "🇨"
@@ -57,33 +58,35 @@ class Trivia:
self.incorrect_emoji = self.bot.get_emoji(421526796379488256) or "❌"
self.emojis = [a_emoji, b_emoji, c_emoji, d_emoji]
self.games = {}
self.error_colour = EmbedColours.dark_red
self.trivia_colour = EmbedColours.blue
self.error_colour = roxbot.EmbedColours.dark_red
self.trivia_colour = roxbot.EmbedColours.blue

# Game Functions

def setup_variables(self, player, channel, args):
amount = "medium"
mobile_comp = False
solo = False
for arg in args:
if "length=" in arg:
amount = arg.split("=")[-1]
elif "mobile" ==arg:
mobile_comp = True
elif "solo" == arg:
solo = True

# Length of game
length = {"short": 5, "medium": 10, "long": 15}
if amount not in length:
def setup_variables(self, player, channel, *args):
parser = roxbot.utils.ArgParser()
parser.add_argument("--mobile", "-m", default=False, action="store_true", dest="mobile")
parser.add_argument("--solo", "-s", default=False, action="store_true", dest="solo")
parser.add_argument("--length", "-l", default="medium", type=str, choices=["short", "medium", "long"], dest="length")
options, unknowns = parser.parse_known_args(args)
try:
amount = options.length
except AttributeError:
amount = "medium"
try:
mobile = options.mobile
except AttributeError:
mobile = False
try:
solo = options.solo
except AttributeError:
solo = False

# Game Dictionaries
game = {
"players": {player.id: 0},
"active": 0,
"length": length[amount],
"length": self.lengths[amount],
"current_question": None,
"players_answered": [],
"correct_users": {},
@@ -91,12 +94,12 @@ class Trivia:
}
self.games[channel.id] = game

kwargs = {"mobile_comp": mobile_comp, "solo": solo}
kwargs = {"mobile_comp": mobile, "solo": solo}

return kwargs

async def get_questions(self, amount=10):
return await http.api_request("https://opentdb.com/api.php?amount={}".format(amount))
return await roxbot.http.api_request("https://opentdb.com/api.php?amount={}".format(amount))

def parse_question(self, question, counter, mobile_comp):
if mobile_comp:
@@ -252,7 +255,7 @@ class Trivia:
# Display Correct answer and calculate and display scores.
index = self.games[channel.id]["correct_answer"]
embed = discord.Embed(
colour=EmbedColours.triv_green,
colour=roxbot.EmbedColours.triv_green,
description="Correct answer is {} **{}**".format(
self.emojis[index],
unescape(question["correct_answer"])
@@ -306,7 +309,7 @@ class Trivia:
async def trivia(self, ctx):
"""Command group for the Roxbot Trivia game."""
if ctx.invoked_subcommand == self.start and ctx.channel.id not in self.games:
embed = discord.Embed(colour=EmbedColours.pink)
embed = discord.Embed(colour=roxbot.EmbedColours.pink)
embed.set_footer(text="Roxbot Trivia uses the Open Trivia DB, made and maintained by Pixeltail Games LLC. Find out more at https://opentdb.com/")
embed.set_image(url="https://i.imgur.com/yhRVl9e.png")
await ctx.send(embed=embed)
@@ -319,13 +322,13 @@ class Trivia:
embed = discord.Embed(
title="About Roxbot Trivia",
description="Roxbot Trivia is a trivia game in *your* discord server. It's heavily inspired by Tower Unite's trivia game. (and even uses the same questions database!) To start, just type `{}trivia start`.".format(self.bot.command_prefix),
colour=EmbedColours.pink)
colour=roxbot.EmbedColours.pink)
embed.add_field(name="How to Play", value="Once the game has started, questions will be asked and you will be given 20 seconds to answer them. To answer, react with the corrosponding emoji. Roxbot will only accept your first answer. Score is calculated by how quickly you can answer correctly, so make sure to be as quick as possible to win! Person with the most score at the end wins. Glhf!")
embed.add_field(name="Can I have shorter or longer games?", value="Yes! You can change the length of the game by adding either short (5 questions) or long (15 questions) at the end of the start command. `{}trivia start length=short`. The default is 10 and this is the medium option.".format(self.bot.command_prefix))
embed.add_field(name="Can I have shorter or longer games?", value="Yes! You can change the length of the game by using the argument `-l or --length` adding short (5 questions), medium (10), or long (15) at the end. `{}trivia start --length short`. The default is medium.".format(self.bot.command_prefix))
embed.add_field(name="Can I play with friends?", value="Yes! Trivia is best with friends. How else would friendships come to their untimely demise? You can only join a game during the 20 second waiting period after a game is started. Just type `{0}trivia join` and you're in! You can leave a game at anytime (even if its just you) by doing `{0}trivia leave`. If no players are in a game, the game will end and no one will win ;-;".format(self.bot.command_prefix))
embed.add_field(name="What if I don't want anyone to join my solo game? Waiting is boring!", value="No problem! Just put `solo` anywhere after `{}trivia start`".format(self.bot.command_prefix))
embed.add_field(name="I can't read the questions on mobile!", value="Sadly this is an issue with Discord on mobile. To get around this, Roxbot Trivia has a mobile compatible version. Just put `mobile` anywhere after `{}trivia start`".format(self.bot.command_prefix))
embed.add_field(name="Can I have a mobile compatible short solo game?", value="Yes, you can use any of the three arguments at once. Just make sure to include a space between them. Example: `{0}tr start mobile solo` or `{0}tr start length=long mobile`".format(self.bot.command_prefix))
embed.add_field(name="What if I don't want anyone to join my solo game? Waiting is boring!", value="No problem! Just put `-s or --solo` anywhere after `{}trivia start`".format(self.bot.command_prefix))
embed.add_field(name="I can't read the questions on mobile!", value="Sadly this is an issue with Discord on mobile. To get around this, Roxbot Trivia has a mobile compatible version. Just put `-m or --mobile` anywhere after `{}trivia start`".format(self.bot.command_prefix))
embed.add_field(name="Can I have a mobile compatible short solo game?", value="Yes, you can use any of the three arguments at once. The Trivia command takes commands just like a cli. Example: `{0}tr start -ms` or `{0}tr start --length long --mobile`".format(self.bot.command_prefix))
embed.set_footer(text="Roxbot Trivia uses the Open Trivia DB, made and maintained by Pixeltail Games LLC. Find out more at https://opentdb.com/")
embed.set_image(url="https://i.imgur.com/yhRVl9e.png")
return await ctx.send(embed=embed)
@@ -344,7 +347,7 @@ class Trivia:
return await ctx.message.delete()

# Setup variables and wait for all players to join.
kwargs = self.setup_variables(player, channel, args)
kwargs = self.setup_variables(player, channel, *args)

# Get questions
questions = await self.get_questions(self.games[channel.id]["length"])
@@ -375,7 +378,7 @@ class Trivia:
final_scores = self.sort_leaderboard(self.games[channel.id]["players"])
winner = self.bot.get_user(list(final_scores.keys())[0])
winning_score = list(final_scores.values())[0]
embed = discord.Embed(description="{} won with a score of {}".format(winner.mention, winning_score), colour=EmbedColours.gold)
embed = discord.Embed(description="{} won with a score of {}".format(winner.mention, winning_score), colour=roxbot.EmbedColours.gold)
await ctx.send(embed=embed)
self.games.pop(channel.id)


Loading…
Cancel
Save