a
This commit is contained in:
parent
d3bfc203b5
commit
39ef4cd5e8
@ -1043,12 +1043,21 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
from discordbot.global_bot_accessor import get_bot_instance
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Try to get the bot instance, but don't require it
|
||||
bot = None
|
||||
try:
|
||||
@ -1108,12 +1117,21 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
from discordbot.global_bot_accessor import get_bot_instance
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Try to get the bot instance, but don't require it
|
||||
bot = None
|
||||
try:
|
||||
|
@ -23,6 +23,7 @@ from discordbot.api_service.dashboard_models import (
|
||||
|
||||
# Import settings_manager for database access (use absolute path)
|
||||
from discordbot import settings_manager
|
||||
from discordbot.global_bot_accessor import get_bot_instance
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@ -40,12 +41,20 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Get command customizations
|
||||
command_customizations = await settings_manager.get_all_command_customizations(guild_id)
|
||||
if command_customizations is None:
|
||||
@ -103,12 +112,20 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Validate custom name format if provided
|
||||
if customization.custom_name is not None:
|
||||
if not customization.custom_name.islower() or not customization.custom_name.replace('_', '').isalnum():
|
||||
@ -179,12 +196,20 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Validate custom name format if provided
|
||||
if customization.custom_name is not None:
|
||||
if not customization.custom_name.islower() or not customization.custom_name.replace('_', '').isalnum():
|
||||
@ -233,12 +258,20 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Validate alias format
|
||||
if not alias.alias_name.islower() or not alias.alias_name.replace('_', '').isalnum():
|
||||
raise HTTPException(
|
||||
@ -286,12 +319,20 @@ 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.get_pg_pool():
|
||||
if not settings_manager:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Settings manager not available"
|
||||
)
|
||||
|
||||
# Get the bot instance to check if pools are available
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database connection not available"
|
||||
)
|
||||
|
||||
# Remove the command alias
|
||||
success = await settings_manager.remove_command_alias(
|
||||
guild_id,
|
||||
|
@ -14,6 +14,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
# Import settings manager for database operations
|
||||
import discordbot.settings_manager as settings_manager
|
||||
from discordbot.global_bot_accessor import get_bot_instance
|
||||
|
||||
# Set up logging
|
||||
log = logging.getLogger(__name__)
|
||||
@ -513,8 +514,14 @@ class StarboardCog(commands.Cog):
|
||||
await ctx.send("❌ Failed to retrieve starboard settings.")
|
||||
return
|
||||
|
||||
# Get the bot instance and its pg_pool
|
||||
bot_instance = get_bot_instance()
|
||||
if not bot_instance or not bot_instance.pg_pool:
|
||||
await ctx.send("❌ Database connection not available.")
|
||||
return
|
||||
|
||||
# Get a connection to the database
|
||||
conn = await asyncio.wait_for(settings_manager.get_pg_pool().acquire(), timeout=5.0)
|
||||
conn = await asyncio.wait_for(bot_instance.pg_pool.acquire(), timeout=5.0)
|
||||
try:
|
||||
# Get the total number of entries
|
||||
total_entries = await conn.fetchval(
|
||||
@ -566,7 +573,7 @@ class StarboardCog(commands.Cog):
|
||||
await ctx.send(embed=embed)
|
||||
finally:
|
||||
# Release the connection
|
||||
await settings_manager.get_pg_pool().release(conn)
|
||||
await bot_instance.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)}")
|
||||
|
@ -7,6 +7,7 @@ import os
|
||||
# Add the parent directory to sys.path to ensure settings_manager is accessible
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
import settings_manager
|
||||
from global_bot_accessor import get_bot_instance
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@ -226,18 +227,12 @@ class WelcomeCog(commands.Cog):
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
# Ensure pools are initialized before adding the cog
|
||||
# Ensure bot has pools initialized before adding the cog
|
||||
print("WelcomeCog setup function called!")
|
||||
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:
|
||||
await settings_manager.initialize_pools()
|
||||
print("WelcomeCog: Settings Manager pools initialized successfully.")
|
||||
except Exception as e:
|
||||
log.exception("Failed to initialize Settings Manager pools during WelcomeCog setup. Cog will not load.")
|
||||
print(f"WelcomeCog: Failed to initialize Settings Manager pools: {e}")
|
||||
return # Prevent loading if pools fail
|
||||
if not hasattr(bot, 'pg_pool') or not hasattr(bot, 'redis') or bot.pg_pool is None or bot.redis is None:
|
||||
log.warning("Bot pools not initialized before loading WelcomeCog. Cog will not load.")
|
||||
print("WelcomeCog: Bot pools not initialized. Cannot load cog.")
|
||||
return # Prevent loading if pools are missing
|
||||
|
||||
welcome_cog = WelcomeCog(bot)
|
||||
await bot.add_cog(welcome_cog)
|
||||
|
Loading…
x
Reference in New Issue
Block a user