aa
This commit is contained in:
parent
c296164b03
commit
bfb21cd169
26
global_bot_accessor.py
Normal file
26
global_bot_accessor.py
Normal file
@ -0,0 +1,26 @@
|
||||
# discordbot/global_bot_accessor.py
|
||||
"""
|
||||
This module provides a global accessor for the bot instance.
|
||||
It helps to avoid circular dependencies when other modules need access
|
||||
to the bot instance, especially its shared resources like connection pools.
|
||||
"""
|
||||
|
||||
_bot_instance = None
|
||||
|
||||
def set_bot_instance(bot_instance_ref):
|
||||
"""
|
||||
Sets the global bot instance. Should be called once from main.py
|
||||
after the bot object is created.
|
||||
"""
|
||||
global _bot_instance
|
||||
if _bot_instance is not None and _bot_instance != bot_instance_ref :
|
||||
# This might indicate an issue if called multiple times with different instances
|
||||
print(f"WARNING: Global bot instance is being overwritten. Old ID: {id(_bot_instance)}, New ID: {id(bot_instance_ref)}")
|
||||
_bot_instance = bot_instance_ref
|
||||
|
||||
def get_bot_instance():
|
||||
"""
|
||||
Retrieves the global bot instance.
|
||||
Returns None if set_bot_instance has not been called.
|
||||
"""
|
||||
return _bot_instance
|
4
main.py
4
main.py
@ -22,6 +22,7 @@ from utils import reload_script
|
||||
import settings_manager # Import the settings manager
|
||||
from db import mod_log_db # Import the new mod log db functions
|
||||
import command_customization # Import command customization utilities
|
||||
from discordbot.global_bot_accessor import set_bot_instance # Import the new accessor
|
||||
|
||||
# Import the unified API service runner and the sync API module
|
||||
import sys
|
||||
@ -558,6 +559,9 @@ async def main(args): # Pass parsed args
|
||||
else:
|
||||
bot.ai_cogs_to_skip = [] # Ensure it exists even if empty
|
||||
|
||||
set_bot_instance(bot) # Set the global bot instance
|
||||
log.info(f"Global bot instance set in global_bot_accessor. Bot ID: {id(bot)}")
|
||||
|
||||
# Pool initialization and cog loading are now handled in MyBot.setup_hook()
|
||||
|
||||
try:
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user