import os import importlib.util import logging logger = logging.getLogger(__name__) def load_commands(): """ Automatically discover and load command modules from bot/commands/. Each module should call register_module() during import. """ commands_dir = os.path.dirname(__file__) for filename in os.listdir(commands_dir): if filename.endswith('.py') and filename != '__init__.py': module_name = filename[:-3] # remove .py module_path = os.path.join(commands_dir, filename) try: spec = importlib.util.spec_from_file_location(module_name, module_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) logger.info(f"Loaded command module: {module_name}") except Exception as e: logger.error(f"Failed to load command module {module_name}: {e}")