aboutsummaryrefslogtreecommitdiff
path: root/minecrafttrivia/trivia_interface_cog.py
blob: b27f789f155d3a2a44dda41554907582c2edd6f7 (plain)
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
import typing

import discord
from discord.ext.commands import guild_only
from redbot.core import commands, Config
from redbot.core.bot import Red

from . import utils
from .game import OngoingGame, CraftingGame, GamePhase


class TriviaInterfaceCog(commands.Cog):
	def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.config = Config.get_conf(self, identifier=262200644)
		self.config.register_global(version=0)
		self.config.register_guild(
			join_timeout=30,
			guess_timeout=60,
			total_scores={},
			high_scores={},
		)
		self.active_games_per_channel: typing.Dict[int, OngoingGame] = {}

	def get_game(self, channel: discord.TextChannel) -> typing.Optional[OngoingGame]:
		game = channel.id in self.active_games_per_channel and self.active_games_per_channel[channel.id]
		if game and game.phase != GamePhase.FINISHED:
			return game

	def create_game(self, bot: Red, channel: discord.TextChannel) -> OngoingGame:
		game = CraftingGame(bot, self.config, channel)
		self.active_games_per_channel[channel.id] = game
		return game

	@commands.command(aliases=["mctrivia", "mct"])
	@guild_only()
	async def minecrafttrivia(self, ctx: commands.GuildContext, action: str = "new"):
		"""Starts a game of minecraft trivia"""
		game = self.get_game(ctx.channel)
		if action == "new":
			if game:
				return await ctx.send("Game already started.")
			game = self.create_game(ctx.bot, ctx.channel)
			await game.start_signup()
		elif action[:4] == "high":
			high_scores = await self.config.guild(ctx.guild).high_scores()
			await ctx.send(embed=discord.Embed(
				title=f"MC Trivia Highscores for {ctx.guild.name}",
				description=utils.format_leaderboard(utils.create_leaderboard(high_scores))))
		elif action[:4] == "lead":
			total_scores = await self.config.guild(ctx.guild).total_scores()
			await ctx.send(embed=discord.Embed(
				title=f"MC Trivia Leaderboard for {ctx.guild.name}",
				description=utils.format_leaderboard(utils.create_leaderboard(total_scores))
			))