From 05a180c2d5392fe5943680857e711ed3921317f3 Mon Sep 17 00:00:00 2001 From: Slipstream Date: Tue, 6 May 2025 18:03:55 -0600 Subject: [PATCH] aa --- api_service/api_server.py | 34 +++++++++---------- api_service/cog_management_endpoints.py | 4 +-- .../command_customization_endpoints.py | 10 +++--- api_service/dashboard_api_endpoints.py | 18 +++++----- cogs/starboard_cog.py | 4 +-- cogs/welcome_cog.py | 2 +- settings_manager.py | 9 +++++ 7 files changed, 45 insertions(+), 36 deletions(-) diff --git a/api_service/api_server.py b/api_service/api_server.py index ebf9a4a..6122f7a 100644 --- a/api_service/api_server.py +++ b/api_service/api_server.py @@ -324,7 +324,7 @@ async def lifespan(_: FastAPI): # Underscore indicates unused but required para try: # Initialize the pools in the settings_manager module # Close any existing pools first to ensure clean state - if settings_manager.pg_pool or settings_manager.redis_pool: + if settings_manager.get_pg_pool() or settings_manager.get_redis_pool(): log.info("Closing existing database pools before reinitializing...") await settings_manager.close_pools() @@ -359,7 +359,7 @@ async def lifespan(_: FastAPI): # Underscore indicates unused but required para log.info("Existing database saved.") # Close database/cache pools if they were initialized - if settings_manager and (settings_manager.pg_pool or settings_manager.redis_pool): + if settings_manager and (settings_manager.get_pg_pool() or settings_manager.get_redis_pool()): log.info("Closing database and cache connection pools for API server...") await settings_manager.close_pools() log.info("Database and cache connection pools closed for API server.") @@ -577,7 +577,7 @@ async def get_guild_cogs_no_deps(guild_id: int): log.warning(f"Could not import bot instance: {e}") # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): return {"error": "Settings manager not available", "cogs": []} # Get cogs from the database directly if bot is not available @@ -875,7 +875,7 @@ async def get_guild_cogs_direct( log.warning(f"Could not import bot instance: {e}") # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -1029,7 +1029,7 @@ async def update_cog_status_direct( """Enable or disable a cog for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -1094,7 +1094,7 @@ async def update_command_status_direct( """Enable or disable a command for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -1816,8 +1816,8 @@ async def dashboard_get_guild_settings( known_cogs_in_db = {} try: # Need to acquire connection from pool managed by settings_manager - if settings_manager.pg_pool: - async with settings_manager.pg_pool.acquire() as conn: + if settings_manager.get_pg_pool(): + async with settings_manager.get_pg_pool().acquire() as conn: records = await conn.fetch("SELECT cog_name, enabled FROM enabled_cogs WHERE guild_id = $1", guild_id) for record in records: known_cogs_in_db[record['cog_name']] = record['enabled'] @@ -1830,8 +1830,8 @@ async def dashboard_get_guild_settings( # Fetch command permissions permissions_map: Dict[str, List[str]] = {} try: - if settings_manager.pg_pool: - async with settings_manager.pg_pool.acquire() as conn: + if settings_manager.get_pg_pool(): + async with settings_manager.get_pg_pool().acquire() as conn: records = await conn.fetch( "SELECT command_name, allowed_role_id FROM command_permissions WHERE guild_id = $1 ORDER BY command_name, allowed_role_id", guild_id @@ -1926,8 +1926,8 @@ async def dashboard_get_all_guild_command_permissions_map( log.info(f"Dashboard: Fetching all command permissions map for guild {guild_id} requested by user {current_user['user_id']}") permissions_map: Dict[str, List[str]] = {} try: - if settings_manager.pg_pool: - async with settings_manager.pg_pool.acquire() as conn: + if settings_manager.get_pg_pool(): + async with settings_manager.get_pg_pool().acquire() as conn: records = await conn.fetch( "SELECT command_name, allowed_role_id FROM command_permissions WHERE guild_id = $1 ORDER BY command_name, allowed_role_id", guild_id @@ -1960,8 +1960,8 @@ async def dashboard_get_all_guild_command_permissions( log.info(f"Dashboard: Fetching all command permissions for guild {guild_id} requested by user {current_user['user_id']}") permissions_list = [] try: - if settings_manager.pg_pool: - async with settings_manager.pg_pool.acquire() as conn: + if settings_manager.get_pg_pool(): + async with settings_manager.get_pg_pool().acquire() as conn: records = await conn.fetch( "SELECT command_name, allowed_role_id FROM command_permissions WHERE guild_id = $1 ORDER BY command_name, allowed_role_id", guild_id @@ -2025,7 +2025,7 @@ async def ai_moderation_action( raise HTTPException(status_code=400, detail="guild_id in path does not match payload") # Insert into moderation log - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): log.error("settings_manager or pg_pool not available for AI moderation logging.") raise HTTPException(status_code=503, detail="Moderation logging unavailable") @@ -2040,7 +2040,7 @@ async def ai_moderation_action( try: from discordbot.db import mod_log_db case_id = await mod_log_db.add_mod_log( - settings_manager.pg_pool, + settings_manager.get_pg_pool(), guild_id=action.guild_id, moderator_id=AI_MODERATOR_ID, target_user_id=action.user_id, @@ -2051,7 +2051,7 @@ async def ai_moderation_action( # Optionally update with message/channel info if case_id and action.message_id and action.channel_id: await mod_log_db.update_mod_log_message_details( - settings_manager.pg_pool, + settings_manager.get_pg_pool(), case_id=case_id, message_id=action.message_id, channel_id=action.channel_id diff --git a/api_service/cog_management_endpoints.py b/api_service/cog_management_endpoints.py index 0aa8c14..8814d30 100644 --- a/api_service/cog_management_endpoints.py +++ b/api_service/cog_management_endpoints.py @@ -106,7 +106,7 @@ async def update_cog_status( """Enable or disable a cog for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -168,7 +168,7 @@ async def update_command_status( """Enable or disable a command for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" diff --git a/api_service/command_customization_endpoints.py b/api_service/command_customization_endpoints.py index f113384..1ca33b3 100644 --- a/api_service/command_customization_endpoints.py +++ b/api_service/command_customization_endpoints.py @@ -40,7 +40,7 @@ async def get_command_customizations( """Get all command customizations for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -103,7 +103,7 @@ async def set_command_customization( """Set a custom name and/or description for a command in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -179,7 +179,7 @@ async def set_group_customization( """Set a custom name for a command group in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -233,7 +233,7 @@ async def add_command_alias( """Add an alias for a command in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -286,7 +286,7 @@ async def remove_command_alias( """Remove an alias for a command in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" diff --git a/api_service/dashboard_api_endpoints.py b/api_service/dashboard_api_endpoints.py index bb67c8f..2913570 100644 --- a/api_service/dashboard_api_endpoints.py +++ b/api_service/dashboard_api_endpoints.py @@ -213,7 +213,7 @@ async def get_command_customizations( """Get all command customizations for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -268,7 +268,7 @@ async def set_command_customization( """Set a custom name for a command in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -322,7 +322,7 @@ async def set_group_customization( """Set a custom name for a command group in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -376,7 +376,7 @@ async def add_command_alias( """Add an alias for a command in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -429,7 +429,7 @@ async def remove_command_alias( """Remove an alias for a command in a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -468,7 +468,7 @@ async def get_guild_settings( """Get settings for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -563,7 +563,7 @@ async def update_guild_settings( """Update settings for a guild.""" try: # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -1034,7 +1034,7 @@ async def update_cog_status_redirect( # Fall back to direct implementation # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" @@ -1125,7 +1125,7 @@ async def update_command_status_redirect( # Fall back to direct implementation # Check if settings_manager is available - if not settings_manager or not settings_manager.pg_pool: + if not settings_manager or not settings_manager.get_pg_pool(): raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Settings manager not available" diff --git a/cogs/starboard_cog.py b/cogs/starboard_cog.py index 5e2a383..5c8d7aa 100644 --- a/cogs/starboard_cog.py +++ b/cogs/starboard_cog.py @@ -514,7 +514,7 @@ class StarboardCog(commands.Cog): return # Get a connection to the database - conn = await asyncio.wait_for(settings_manager.pg_pool.acquire(), timeout=5.0) + conn = await asyncio.wait_for(settings_manager.get_pg_pool().acquire(), timeout=5.0) try: # Get the total number of entries total_entries = await conn.fetchval( @@ -566,7 +566,7 @@ class StarboardCog(commands.Cog): await ctx.send(embed=embed) finally: # Release the connection - await settings_manager.pg_pool.release(conn) + await settings_manager.get_pg_pool().release(conn) except Exception as e: log.exception(f"Error getting starboard statistics: {e}") await ctx.send(f"❌ An error occurred while getting starboard statistics: {str(e)}") diff --git a/cogs/welcome_cog.py b/cogs/welcome_cog.py index f26a930..b5bd461 100644 --- a/cogs/welcome_cog.py +++ b/cogs/welcome_cog.py @@ -228,7 +228,7 @@ class WelcomeCog(commands.Cog): async def setup(bot: commands.Bot): # Ensure pools are initialized before adding the cog print("WelcomeCog setup function called!") - if settings_manager.pg_pool is None or settings_manager.redis_pool is None: + if settings_manager.get_pg_pool() is None or settings_manager.get_redis_pool() is None: log.warning("Settings Manager pools not initialized before loading WelcomeCog. Attempting initialization.") print("WelcomeCog: Settings Manager pools not initialized, attempting initialization...") try: diff --git a/settings_manager.py b/settings_manager.py index 6e1fac8..942a86c 100644 --- a/settings_manager.py +++ b/settings_manager.py @@ -2113,3 +2113,12 @@ async def set_mod_log_channel_id(guild_id: int, channel_id: int | None) -> bool: """Sets the channel ID for the integrated moderation log. Set to None to disable.""" value_to_set = str(channel_id) if channel_id is not None else None return await set_setting(guild_id, 'mod_log_channel_id', value_to_set) + +# --- Getter functions for direct pool access if absolutely needed --- +def get_pg_pool(): + """Returns the active PostgreSQL pool instance.""" + return _active_pg_pool + +def get_redis_pool(): + """Returns the active Redis pool instance.""" + return _active_redis_pool