from bson.errors import InvalidId from discord import ApplicationContext, Cog, option, slash_command from discord.utils import basic_autocomplete from libbot.i18n import _, in_every_locale from classes import PycordEvent, PycordGuild, PycordUser from classes.errors import EventNotFoundError, GuildNotFoundError from classes.pycord_bot import PycordBot from modules.utils import autocomplete_user_registered_events, is_operation_confirmed class CogUnregister(Cog): """Cog with the event unregistration command.""" def __init__(self, bot: PycordBot): self.bot: PycordBot = bot @slash_command( name="unregister", description=_("description", "commands", "unregister"), description_localizations=in_every_locale("description", "commands", "unregister"), ) @option( "event", description=_("description", "commands", "unregister", "options", "event"), description_localizations=in_every_locale( "description", "commands", "unregister", "options", "event" ), autocomplete=basic_autocomplete(autocomplete_user_registered_events), ) @option( "confirm", description=_("description", "commands", "unregister", "options", "confirm"), description_localizations=in_every_locale( "description", "commands", "unregister", "options", "confirm" ), required=False, ) async def command_unregister(self, ctx: ApplicationContext, event: str, 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 try: pycord_event: PycordEvent = await self.bot.find_event(event_id=event) except (InvalidId, EventNotFoundError): await ctx.respond(self.bot._("event_not_found", "messages", locale=ctx.locale), ephemeral=True) return if not guild.is_configured(): await ctx.respond( self.bot._("guild_unconfigured", "messages", locale=ctx.locale), ephemeral=True ) return user: PycordUser = await self.bot.find_user(ctx.author, ctx.guild) if user.is_jailed: await ctx.respond(self.bot._("jailed_error", "messages", locale=ctx.locale), ephemeral=True) return # TODO Fix a bug where registered_event_ids is invalid because of caching if pycord_event._id not in user.registered_event_ids: await ctx.respond( self.bot._("unregister_not_registered", "messages", locale=ctx.locale), ephemeral=True ) return await user.event_unregister(pycord_event._id, cache=self.bot.cache) # TODO Text channel must be locked and updated await ctx.respond(self.bot._("unregister_unregistered", "messages", locale=ctx.locale)) def setup(bot: PycordBot) -> None: bot.add_cog(CogUnregister(bot))