"""Cache management commands cog.""" from __future__ import annotations import disnake from disnake.ext import commands from loguru import logger from sdk import SummarizerAPIError, SummarizerClient from src.bot import SummarizerBot from src.embeds import cache_entry_embed, cache_stats_embed, error_embed, success_embed class CacheCog(commands.Cog): """Commands for cache management.""" def __init__(self, bot: SummarizerBot) -> None: self.bot = bot @commands.slash_command(name="cache", description="Cache management commands") async def cache( self, inter: disnake.ApplicationCommandInteraction[SummarizerBot] ) -> None: """Parent command for cache operations.""" @cache.sub_command(name="stats", description="View cache statistics") async def cache_stats( self, inter: disnake.ApplicationCommandInteraction[SummarizerBot] ) -> None: """Display cache statistics.""" await inter.response.defer() try: async with SummarizerClient(base_url=self.bot.config.bot.api_url) as client: stats = await client.get_cache_stats() await inter.edit_original_response(embed=cache_stats_embed(stats)) except SummarizerAPIError as e: logger.warning(f"API error getting cache stats: {e}") await inter.edit_original_response( embed=error_embed("Failed to Get Stats", str(e)) ) except Exception as e: # noqa: BLE001 logger.exception("Unexpected error getting cache stats") await inter.edit_original_response( embed=error_embed("Something went wrong", str(e)) ) @cache.sub_command(name="list", description="List your cached summaries") async def cache_list( self, inter: disnake.ApplicationCommandInteraction[SummarizerBot], limit: int = commands.Param( description="Number of entries to show", default=5, ge=1, le=25 ), ) -> None: """List user's cached summaries.""" await inter.response.defer(ephemeral=True) try: async with SummarizerClient(base_url=self.bot.config.bot.api_url) as client: entries = await client.get_user_cache(str(inter.author.id)) if not entries: await inter.edit_original_response( embed=success_embed( "No Cache Entries", "You don't have any cached summaries yet." ) ) return # Show limited entries entries = entries[:limit] embeds = [cache_entry_embed(entry) for entry in entries] await inter.edit_original_response(embeds=embeds[:10]) # Discord limit except SummarizerAPIError as e: logger.warning(f"API error listing cache: {e}") await inter.edit_original_response( embed=error_embed("Failed to List Cache", str(e)) ) except Exception as e: # noqa: BLE001 logger.exception("Unexpected error listing cache") await inter.edit_original_response( embed=error_embed("Something went wrong", str(e)) ) @cache.sub_command(name="clear", description="Clear your cached summaries") async def cache_clear( self, inter: disnake.ApplicationCommandInteraction[SummarizerBot] ) -> None: """Clear user's cached summaries.""" await inter.response.defer(ephemeral=True) try: async with SummarizerClient(base_url=self.bot.config.bot.api_url) as client: result = await client.delete_cache(user_id=str(inter.author.id)) await inter.edit_original_response( embed=success_embed("Cache Cleared", result.message) ) except SummarizerAPIError as e: logger.warning(f"API error clearing cache: {e}") await inter.edit_original_response( embed=error_embed("Failed to Clear Cache", str(e)) ) except Exception as e: # noqa: BLE001 logger.exception("Unexpected error clearing cache") await inter.edit_original_response( embed=error_embed("Something went wrong", str(e)) ) def setup(bot: SummarizerBot) -> None: """Load the cog.""" bot.add_cog(CacheCog(bot))