from zoneinfo import ZoneInfo, ZoneInfoNotFoundError from discord import ( ApplicationContext, CategoryChannel, SlashCommandGroup, TextChannel, option, ) from discord.ext.commands import Cog from discord.utils import basic_autocomplete from classes import PycordGuild from classes.pycord_bot import PycordBot from modules.utils import autofill_timezones, autofill_languages 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", autocomplete=basic_autocomplete(autofill_timezones), required=True, ) @option( "language", description="Language for bot's messages", autocomplete=basic_autocomplete(autofill_languages), required=True, ) async def command_config_set( self, ctx: ApplicationContext, category: CategoryChannel, channel: TextChannel, timezone: str, language: 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.update( self.bot.cache, channel_id=channel.id, category_id=category.id, timezone=str(timezone_parsed), language=language, ) # 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.purge(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_show(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}>\nTimezone: {guild.timezone}\nLanguage: {guild.language}" ) def setup(bot: PycordBot) -> None: bot.add_cog(Config(bot))