Created /core module with specialized files:

base.py: Main cog class and initialization logic
cleanup.py: Resource cleanup functionality
commands.py: Command handlers
events.py: Event listeners
guild.py: Guild component management
init.py: Module exports
Improved code organization by:

Separating concerns into focused modules
Maintaining clear dependencies between modules
Keeping related functionality together
Making the codebase more maintainable
Preserved all existing functionality while making the code more modular and easier to maintain.
This commit is contained in:
pacnpal
2024-11-15 20:01:45 +00:00
parent 434911abf0
commit 42b3ceb314
11 changed files with 747 additions and 1020 deletions

View File

@@ -0,0 +1,74 @@
"""Guild component management for VideoArchiver"""
import logging
from typing import TYPE_CHECKING
from ..utils.video_downloader import VideoDownloader
from ..utils.message_manager import MessageManager
from ..utils.file_ops import cleanup_downloads
from ..utils.exceptions import VideoArchiverError as ProcessingError
if TYPE_CHECKING:
from .base import VideoArchiver
logger = logging.getLogger("VideoArchiver")
async def initialize_guild_components(cog: "VideoArchiver", guild_id: int) -> None:
"""Initialize or update components for a guild with error handling"""
try:
settings = await cog.config_manager.get_guild_settings(guild_id)
# Ensure download directory exists and is clean
cog.download_path.mkdir(parents=True, exist_ok=True)
await cleanup_downloads(str(cog.download_path))
# Clean up old components if they exist
if guild_id in cog.components:
old_components = cog.components[guild_id]
if "message_manager" in old_components:
await old_components["message_manager"].cancel_all_deletions()
if "downloader" in old_components:
old_components["downloader"] = None
# Initialize new components with validated settings
cog.components[guild_id] = {
"downloader": VideoDownloader(
str(cog.download_path),
settings["video_format"],
settings["video_quality"],
settings["max_file_size"],
settings["enabled_sites"] if settings["enabled_sites"] else None,
settings["concurrent_downloads"],
ffmpeg_mgr=cog.ffmpeg_mgr, # Use shared FFmpeg manager
),
"message_manager": MessageManager(
settings["message_duration"], settings["message_template"]
),
}
logger.info(f"Successfully initialized components for guild {guild_id}")
except Exception as e:
logger.error(f"Failed to initialize guild {guild_id}: {str(e)}")
raise ProcessingError(f"Guild initialization failed: {str(e)}")
async def cleanup_guild_components(cog: "VideoArchiver", guild_id: int) -> None:
"""Clean up components for a specific guild"""
try:
if guild_id in cog.components:
# Clean up components
components = cog.components[guild_id]
if "message_manager" in components:
await components["message_manager"].cancel_all_deletions()
if "downloader" in components:
components["downloader"] = None
if "ffmpeg_mgr" in components:
components["ffmpeg_mgr"] = None
# Remove guild components
cog.components.pop(guild_id)
logger.info(f"Cleaned up components for guild {guild_id}")
except Exception as e:
logger.error(f"Error cleaning up guild {guild_id}: {str(e)}")
raise ProcessingError(f"Guild cleanup failed: {str(e)}")