import contextlib import logging from argparse import ArgumentParser from os import getpid from pathlib import Path from convopyro import Conversation from libbot import sync from classes.pyroclient import PyroClient from modules.database import cursor from modules.migrator import migrate_database from modules.scheduler import scheduler logging.basicConfig( level=logging.INFO, format="%(name)s.%(funcName)s | %(levelname)s | %(message)s", datefmt="[%X]", ) logger = logging.getLogger(__name__) parser = ArgumentParser( prog="BWTAqua Bot", description="Small web scraper for BWT cards' balance parsing", ) parser.add_argument("--migrate", action="store_true") args = parser.parse_args() with contextlib.suppress(ImportError): import uvloop uvloop.install() def main(): client = PyroClient( scheduler=scheduler, commands_source=sync.json_read(Path("commands.json")) ) Conversation(client) if args.migrate: migrate_database() elif Path("data/database.json").exists(): logger.info( "You have an old unmigrated JSON database. Start the bot with --migrate argument to migrate the database to SQLite." ) try: client.run() except KeyboardInterrupt: logger.warning("Forcefully shutting down with PID %s...", getpid()) finally: if client.scheduler is not None: client.scheduler.shutdown() cursor.close() cursor.connection.commit() cursor.connection.close() exit() if __name__ == "__main__": main()