PYRSS-Bot/src/main.py
corbz a9ccf0f791 Greatly improved logging
now using a config file for logging to the terminal and log file

---

Also added custom exception for a missing token.
2024-02-26 18:08:01 +00:00

65 lines
1.8 KiB
Python

"""
Entry point for the application.
Run this file to get started.
"""
import json
import atexit
import asyncio
import logging
import logging.config
from os import getenv
from pathlib import Path
# it's important to load environment variables before
# importing the modules that depend on them.
from dotenv import load_dotenv
load_dotenv(override=True)
from bot import DiscordBot
from errors import TokenMissingError
BASE_DIR = Path(__file__).resolve().parent.parent
async def main():
"""
point function for the application.
Run this function to get started.
"""
# Discord Bot token
bot_token = getenv("BOT_TOKEN")
if not bot_token:
raise TokenMissingError("'BOT_TOKEN' environment variable cannot be missing or blank.")
# Web API token
api_token = getenv("API_TOKEN")
if not api_token:
raise TokenMissingError("'API_TOKEN' environment variable cannot be missing or blank.")
# Effectively debug mode, defaults to True
developing = getenv("DEVELOPING", "False") == "True"
# Logging setup
log_config_path = BASE_DIR / "logs" / "config.json"
if not log_config_path.exists():
raise FileNotFoundError(log_config_path)
with open(log_config_path, "r", encoding="utf-8") as file:
log_config = json.load(file)
logging.config.dictConfig(log_config)
# start the logging queue handler thread
queue_handler = logging.getHandlerByName("queue_handler")
if queue_handler is not None:
queue_handler.listener.start()
atexit.register(queue_handler.listener.stop)
async with DiscordBot(BASE_DIR, developing=developing, api_token=api_token) as bot:
await bot.load_extensions()
await bot.start(bot_token, reconnect=True)
if __name__ == "__main__":
asyncio.run(main())