import os import sys import discord from argparse import ArgumentParser # Parse minimal CLI args early so we can configure logging before imports that # initialize loggers. Keep only `--log-level`; other args were unused. PARSER = ArgumentParser(description='BotMafieux for Discord.') PARSER.add_argument('--log-level', type=str, help='Logging level (DEBUG, INFO, WARNING, ERROR)') EXEC_ARGS = PARSER.parse_args() if EXEC_ARGS.log_level: os.environ['LOG_LEVEL'] = str(EXEC_ARGS.log_level) from setup.bot import bot, reload_feature, register_cogs from setup.logger import LOGGER from setup.config import Config # Register cogs from configuration (no CLI activation) register_cogs() @bot.event async def on_ready(): if bot.user: LOGGER.info(f'{bot.user.name} has connected to Discord!') LOGGER.info(f"Guilds: {','.join([guild.name for guild in bot.guilds])}") # Log configuration for each guild we are connected to try: for guild in bot.guilds: cfg = Config.GUILD_CONFIGS.get(guild.id) if cfg is not None: LOGGER.info(f"Guild config for {guild.name} ({guild.id}): {cfg}") else: LOGGER.info(f"No guild config for {guild.name} ({guild.id})") except Exception as e: LOGGER.warning(f"Failed to log guild configs: {e}") @bot.event async def on_guild_join(guild: discord.Guild): # Refresh configs and log the joined guild configuration try: Config.load_guilds_configs() except Exception: LOGGER.debug('Could not reload guild configs during on_guild_join.') cfg = Config.GUILD_CONFIGS.get(guild.id) if cfg is not None: LOGGER.info(f"Joined guild {guild.name} ({guild.id}) with config: {cfg}") else: LOGGER.info(f"Joined guild {guild.name} ({guild.id}) with no configuration present.") if __name__ == '__main__': token = Config.TOKEN if token: bot.run(token) else: LOGGER.error('No token found.') sys.exit(1)