nonsensebot/app/__main__.py
2024-09-08 01:33:31 +01:00

41 lines
1.2 KiB
Python

import click
import logging
from tortoise import run_async
log = logging.getLogger(__name__)
@click.group()
@click.option(
"--log-level",
default="INFO",
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False),
help="Set the log level."
)
@click.option("--log-file", default=None, type=click.Path(), help="Log to a file as well as stdout")
def cli(log_level: str, log_file: str | None):
logging.basicConfig(
level=log_level.upper(),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
if log_file is not None:
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logging.getLogger().addHandler(file_handler)
@cli.command()
def run():
"""Runs the bot"""
log.info("Starting bot.")
from .main import bot
# run_async(bot.start(access_token=bot.cfg["bot"]["access_token"]))
bot.run(access_token=bot.cfg["bot"]["access_token"])
if __name__ == "__main__":
cli()