fix: Add error handling to all cogs' initialization - Add proper logging - Add teardown functions - Add cleanup on failure

This commit is contained in:
pacnpal
2024-11-15 01:04:53 +00:00
parent 484b0c0cb8
commit 8013be2a66
2 changed files with 37 additions and 8 deletions

View File

@@ -1,13 +1,31 @@
"""Birthday cog for Red-DiscordBot"""
from redbot.core.bot import Red
import logging
from .birthday import Birthday
logger = logging.getLogger("Birthday")
async def setup(bot: Red) -> None:
"""Load Birthday cog."""
cog = Birthday(bot)
await bot.add_cog(cog)
try:
cog = Birthday(bot)
await bot.add_cog(cog)
# Initialize scheduled tasks
try:
await cog.reload_scheduled_tasks()
except Exception as e:
logger.error(f"Failed to initialize scheduled tasks: {str(e)}")
await bot.remove_cog(cog.__class__.__name__)
raise
except Exception as e:
logger.error(f"Failed to load Birthday cog: {str(e)}")
raise
async def teardown(bot: Red) -> None:
"""Clean up when unloading."""
# Let Red handle command cleanup
pass
try:
if "Birthday" in bot.cogs:
await bot.remove_cog("Birthday")
except Exception as e:
logger.error(f"Error during teardown: {str(e)}")
raise