import json import atexit import asyncio import logging import logging.config from os import getenv from pathlib import Path from dotenv import load_dotenv load_dotenv(override=True) from bot.bot import DiscordBot from web.app import create_app BASE_DIR = Path(__file__).parent async def start_web(): app = create_app() await app.run_task(host="0.0.0.0", port=5000) async def start_bot(token: str): async with DiscordBot() as bot: await bot.load_cogs(BASE_DIR / "bot" / "cogs") await bot.start(token, reconnect=True) def setup_logging(): # load config from file log_config_path = BASE_DIR / "logs" / "config.json" if not log_config_path.exists(): raise FileNotFoundError("Logging config not found") with open(log_config_path, "r") as file: logging_config = json.load(file) logging.config.dictConfig(logging_config) # create queue handler for non-blocking logs queue_handler = logging.getHandlerByName("queue_handler") if queue_handler is not None: queue_handler.listener.start() atexit.register(queue_handler.listener.stop) async def main(): bot_token = getenv("BOT_TOKEN") if not bot_token: raise ValueError("'BOT_TOKEN' is missing") setup_logging() await asyncio.gather(start_web(), start_bot(bot_token)) if __name__ == "__main__": asyncio.run(main())