nonsensebot/app/__main__.py

89 lines
2.6 KiB
Python

import asyncio
import copy
import time
import click
import logging
import niobot
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()
@click.option(
"--resume/--no-resume",
default=True,
help="Whether to do a full sync, or resume from previous",
)
def run(resume: bool):
"""Runs the bot"""
log.info("Starting bot.")
log.debug("Importing instance...")
from .main import bot
log.debug("Import complete.")
if resume is False:
log.debug("Resume is false, hijacking config...")
original_config = copy.copy(bot.config)
new_config = niobot.AsyncClientConfig(
store=original_config.store,
encryption_enabled=original_config.encryption_enabled,
store_name=original_config.store_name,
pickle_key=original_config.pickle_key,
store_sync_tokens=False,
custom_headers=original_config.custom_headers,
max_limit_exceeded=original_config.max_limit_exceeded,
max_timeouts=original_config.max_timeouts,
backoff_factor=original_config.backoff_factor,
max_timeout_retry_wait_time=original_config.max_timeout_retry_wait_time,
request_timeout=original_config.request_timeout,
io_chunk_size=original_config.io_chunk_size,
)
bot.config = new_config
bot.load_store()
bot.loaded_sync_token = ""
log.debug("Bot is now prepared for a complete sync")
log.critical("Not resuming from previous sync. This may be slow!")
time.sleep(3)
# run_async(bot.start(access_token=bot.cfg["bot"]["access_token"]))
log.debug("All pistons firing")
asyncio.run(bot.start(access_token=bot.cfg["bot"]["access_token"]))
if __name__ == "__main__":
cli()