From 770e74d63c46ceddb834059d632c0336995de06c Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:57:24 +0000 Subject: [PATCH] fix: Improve command registration and cleanup - Add proper command syncing - Add teardown function - Add error handling for sync operations --- videoarchiver/__init__.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/videoarchiver/__init__.py b/videoarchiver/__init__.py index 1a453fb..1fb558c 100644 --- a/videoarchiver/__init__.py +++ b/videoarchiver/__init__.py @@ -1,12 +1,30 @@ """VideoArchiver cog for Red-DiscordBot""" from redbot.core.bot import Red from .video_archiver import VideoArchiver -from .commands import VideoArchiverCommands async def setup(bot: Red) -> None: """Load VideoArchiver.""" cog = VideoArchiver(bot) + # Add cog first await bot.add_cog(cog) - # Add commands from VideoArchiverCommands - commands_cog = VideoArchiverCommands(bot, cog.config_manager, cog.update_checker, cog.processor) - await bot.add_cog(commands_cog) + # Then sync commands after cog is loaded + if hasattr(bot, "tree"): + try: + # Only sync guild commands to avoid rate limits + await bot.tree.sync(guild=None) + except Exception as e: + # Log error but don't fail cog load + bot.log.error(f"Failed to sync commands: {str(e)}") + +async def teardown(bot: Red) -> None: + """Clean up when unloading.""" + if hasattr(bot, "tree"): + try: + # Remove all commands from this cog + for command in bot.tree.get_commands(): + bot.tree.remove_command(command.name) + # Sync to remove commands from Discord + await bot.tree.sync(guild=None) + except Exception as e: + # Log error but don't fail cog unload + bot.log.error(f"Failed to cleanup commands: {str(e)}")