Files
QuizBot/cogs/config.py
2025-04-18 23:34:11 +02:00

88 lines
2.9 KiB
Python

from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from discord import (
ApplicationContext,
CategoryChannel,
SlashCommandGroup,
TextChannel,
option,
)
from discord.ext.commands import Cog
from classes import PycordGuild
from classes.pycord_bot import PycordBot
class Config(Cog):
"""Cog with guild configuration commands."""
def __init__(self, bot: PycordBot):
self.bot: PycordBot = bot
# TODO Introduce i18n
command_group: SlashCommandGroup = SlashCommandGroup("config", "Guild management")
# TODO Introduce i18n
@command_group.command(
name="set",
description="Configure the guild",
)
@option("category", description="Category where channels for each user will be created", required=True)
@option("channel", description="Text channel for admin notifications", required=True)
@option("timezone", description="Timezone in which events take place", required=True)
async def command_config_set(
self, ctx: ApplicationContext, category: CategoryChannel, channel: TextChannel, timezone: str
) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
try:
timezone_parsed: ZoneInfo = ZoneInfo(timezone)
except ZoneInfoNotFoundError:
await ctx.respond(f"Timezone {timezone} was not found.")
return
await guild.set_channel(channel.id, cache=self.bot.cache)
await guild.set_category(category.id, cache=self.bot.cache)
await guild.set_timezone(str(timezone_parsed), cache=self.bot.cache)
# TODO Make a nice message
await ctx.respond("Okay.")
# TODO Introduce i18n
@command_group.command(
name="reset",
description="Reset the guild's configuration",
)
@option("confirm", description="Confirmation of the operation", required=False)
async def command_config_reset(self, ctx: ApplicationContext, confirm: bool = False) -> None:
if confirm is None or not confirm:
# TODO Make a nice message
await ctx.respond("Operation not confirmed.")
return
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
await guild.reset_channel(cache=self.bot.cache)
await guild.reset_category(cache=self.bot.cache)
await guild.reset_timezone(cache=self.bot.cache)
# TODO Make a nice message
await ctx.respond("Okay.")
# TODO Introduce i18n
@command_group.command(
name="show",
description="Show the guild's configuration",
)
async def command_config_reset(self, ctx: ApplicationContext) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
# TODO Make a nice message
await ctx.respond(
f"**Guild config**\n\nChannel: <#{guild.channel_id}>\nCategory: <#{guild.category_id}>"
)
def setup(bot: PycordBot) -> None:
bot.add_cog(Config(bot))