from zoneinfo import ZoneInfo, ZoneInfoNotFoundError from bson.errors import InvalidId from discord import ( ApplicationContext, CategoryChannel, SlashCommandGroup, TextChannel, option, ) from discord.ext.commands import Cog from discord.utils import basic_autocomplete from libbot.i18n import _, in_every_locale from classes import PycordGuild from classes.errors import GuildNotFoundError from classes.pycord_bot import PycordBot from modules.utils import autocomplete_timezones, is_operation_confirmed class CogConfig(Cog): """Cog with guild configuration commands.""" def __init__(self, bot: PycordBot): self.bot: PycordBot = bot command_group: SlashCommandGroup = SlashCommandGroup( name="config", description=_("description", "commands", "config"), description_localizations=in_every_locale("description", "commands", "config"), ) @command_group.command( name="set", description=_("description", "commands", "config_set"), description_localizations=in_every_locale("description", "commands", "config_set"), ) @option( "category", description=_("description", "commands", "config_set", "options", "category"), description_localizations=in_every_locale( "description", "commands", "config_set", "options", "category" ), required=True, ) @option( "general_channel", description=_("description", "commands", "config_set", "options", "general_channel"), description_localizations=in_every_locale( "description", "commands", "config_set", "options", "general_channel" ), required=True, ) @option( "management_channel", description=_("description", "commands", "config_set", "options", "management_channel"), description_localizations=in_every_locale( "description", "commands", "config_set", "options", "management_channel" ), required=True, ) @option( "timezone", description=_("description", "commands", "config_set", "options", "timezone"), description_localizations=in_every_locale( "description", "commands", "config_set", "options", "timezone" ), autocomplete=basic_autocomplete(autocomplete_timezones), required=True, ) @option( "prefer_emojis", description=_("description", "commands", "config_set", "options", "prefer_emojis"), description_localizations=in_every_locale( "description", "commands", "config_set", "options", "prefer_emojis" ), required=True, ) async def command_config_set( self, ctx: ApplicationContext, category: CategoryChannel, general_channel: TextChannel, management_channel: TextChannel, timezone: str, prefer_emojis: bool, ) -> None: try: guild: PycordGuild = await self.bot.find_guild(ctx.guild.id) except (InvalidId, GuildNotFoundError): await ctx.respond(self.bot._("unexpected_error", "messages", locale=ctx.locale), ephemeral=True) return try: timezone_parsed: ZoneInfo = ZoneInfo(timezone) except ZoneInfoNotFoundError: await ctx.respond( self.bot._("timezone_invalid", "messages", locale=ctx.locale).format(timezone=timezone), ephemeral=True, ) return await guild.update( self.bot.cache, general_channel_id=general_channel.id, management_channel_id=management_channel.id, category_id=category.id, timezone=str(timezone_parsed), prefer_emojis=prefer_emojis, ) await ctx.respond(self.bot._("config_set", "messages", locale=ctx.locale)) @command_group.command( name="reset", description=_("description", "commands", "config_reset"), description_localizations=in_every_locale("description", "commands", "config_reset"), ) @option( "confirm", description=_("description", "commands", "config_reset", "options", "confirm"), description_localizations=in_every_locale( "description", "commands", "config_reset", "options", "confirm" ), required=False, ) async def command_config_reset(self, ctx: ApplicationContext, confirm: bool = False) -> None: if not (await is_operation_confirmed(ctx, confirm)): return try: guild: PycordGuild = await self.bot.find_guild(ctx.guild.id) except (InvalidId, GuildNotFoundError): await ctx.respond(self.bot._("unexpected_error", "messages", locale=ctx.locale), ephemeral=True) return await guild.purge(self.bot.cache) await ctx.respond(self.bot._("config_reset", "messages", locale=ctx.locale)) @command_group.command( name="show", description=_("description", "commands", "config_show"), description_localizations=in_every_locale("description", "commands", "config_show"), ) async def command_config_show(self, ctx: ApplicationContext) -> None: try: guild: PycordGuild = await self.bot.find_guild(ctx.guild.id) except (InvalidId, GuildNotFoundError): await ctx.respond(self.bot._("unexpected_error", "messages", locale=ctx.locale), ephemeral=True) return if not guild.is_configured(): await ctx.respond( self.bot._("guild_unconfigured_admin", "messages", locale=ctx.locale), ephemeral=True ) return await ctx.respond( self.bot._("config_show", "messages", locale=ctx.locale).format( general_channel_id=guild.general_channel_id, management_channel_id=guild.management_channel_id, category_id=guild.category_id, timezone=guild.timezone, prefer_emojis=guild.prefer_emojis, ) ) def setup(bot: PycordBot) -> None: bot.add_cog(CogConfig(bot))