import argparse import asyncio import logging import os from pathlib import Path import sys from .config import ServerConfig from .bot import Bot parser = argparse.ArgumentParser(description="Run an IRC bot") parser.add_argument("-c", "--config", type=str, default="config.toml") parser.add_argument( "--loglevel", type=str, choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], required=False, ) args = parser.parse_args() config_path = Path(os.environ.get("OMNIBOT_CONFIG", args.config)) config = ServerConfig() try: with open(config_path) as fp: config.load(fp) except FileNotFoundError: print(f"ERROR: config file not found: {config_path}", file=sys.stderr) sys.exit(1) # Set log level now loglevel = logging.INFO if args.loglevel: loglevel = getattr(logging, args.loglevel) elif config.loglevel: loglevel = getattr(logging, config.loglevel) logging.basicConfig( level=loglevel, format="%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s", ) log = logging.getLogger(__name__) log.info("Loaded config file %s", args.config) log.debug("Using configuration: %s", config) bot = Bot(config) try: asyncio.run(bot.run()) except KeyboardInterrupt: log.info("Got ctrl-c") finally: log.info("Quitting, press ctrl-c to quit immediately") bot.quit() asyncio.run(bot.keepalive())