1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
import logging
from json import loads
from random import choice
from discord import Embed
from discord.ext import commands
from bot.bot import Bot
from bot.constants import Colours, POSITIVE_REPLIES
from ._questions import QuestionView, Questions
from ._scoreboard import Scoreboard, ScoreboardView
class TriviaNight(commands.Cog):
"""Cog for the Python Trivia Night event."""
def __init__(self, bot: Bot):
self.bot = bot
self.scoreboard = Scoreboard()
self.questions = Questions(self.scoreboard)
@commands.group()
async def trivianight(self, ctx: commands.Context) -> None:
"""No-op subcommand group for organizing different commands."""
return
@trivianight.command()
async def load(self, ctx: commands.Context) -> None:
"""Load the JSON file provided into the questions."""
json_text = (await ctx.message.attachments[0].read()).decode("utf8")
serialized_json = loads(json_text)
self.questions.view = QuestionView()
logging.getLogger(__name__).debug(self.questions.view)
self.scoreboard.view = ScoreboardView(self.bot)
self.questions.set_questions(serialized_json)
success_embed = Embed(
title=choice(POSITIVE_REPLIES),
description="The JSON was loaded successfully!",
color=Colours.soft_green
)
await ctx.send(embed=success_embed)
@trivianight.command()
async def reset(self, ctx: commands.Context) -> None:
"""Resets previous questions and scoreboards."""
self.scoreboard.view = ScoreboardView(self.bot)
for question in self.questions.questions:
del question["visited"]
success_embed = Embed(
title=choice(POSITIVE_REPLIES),
description="The scoreboards were reset and questions marked unvisited!",
color=Colours.soft_green
)
await ctx.send(embed=success_embed)
@trivianight.command()
async def next(self, ctx: commands.Context) -> None:
"""Gets a random question from the unanswered question list and lets user choose the answer."""
next_question = self.questions.next_question()
if isinstance(next_question, Embed):
await ctx.send(embed=next_question)
return
question_embed, question_view = self.questions.current_question()
await ctx.send(embed=question_embed, view=question_view)
@trivianight.command()
async def question(self, ctx: commands.Context, question_number: int) -> None:
"""Gets a question from the question bank depending on the question number provided."""
question = self.questions.next_question(question_number)
if isinstance(question, Embed):
await ctx.send(embed=question)
return
question_embed, question_view = self.questions.current_question()
await ctx.send(embed=question_embed, view=question_view)
@trivianight.command()
async def list(self, ctx: commands.Context) -> None:
"""Displays all the questions from the question bank."""
formatted_string = self.questions.list_questions()
await ctx.send(formatted_string)
@trivianight.command()
async def stop(self, ctx: commands.Context) -> None:
"""End the ongoing question to show the correct question."""
await ctx.send(embed=self.questions.end_question())
@trivianight.command()
async def end(self, ctx: commands.Context) -> None:
"""Ends the trivia night event and displays the scoreboard."""
scoreboard_embed, scoreboard_view = await self.scoreboard.display()
await ctx.send(embed=scoreboard_embed, view=scoreboard_view)
def setup(bot: Bot) -> None:
"""Load the TriviaNight cog."""
bot.add_cog(TriviaNight(bot))
|