Files
QuizBot/main.py

81 lines
2.4 KiB
Python

import contextlib
import logging.config
from argparse import ArgumentParser
from logging import Logger
from os import makedirs
from pathlib import Path
from sys import exit
from discord import Intents, LoginFailure
from libbot.utils import config_get
from classes.pycord_bot import PycordBot
from modules.migrator import migrate_database
from modules.scheduler import scheduler
from modules.utils import get_logger, get_logging_config
makedirs(Path("logs/"), exist_ok=True)
logging.config.dictConfig(get_logging_config())
logger: Logger = get_logger(__name__)
# Declare the parser that retrieves the command line arguments
parser: ArgumentParser = ArgumentParser(prog="QuizBot")
# Add a switch argument --migrate to be parsed...
parser.add_argument("--migrate", action="store_true")
# parser.add_argument("--downgrade", action="store_true")
# parser.add_argument("--confirm", action="store_true")
# ...and parse the arguments we added
args = parser.parse_args()
# Try to import the module that improves performance
# and ignore errors when module is not installed
with contextlib.suppress(ImportError):
import uvloop
uvloop.install()
def main():
# Perform migration if command line argument was provided
if args.migrate:
migrate_database()
# if args.downgrade:
# if not args.confirm:
# logger.warning(
# "Argument --downgrade has been provided but this operation may include irreversible changes and destroy existing data in both database and configuration. It's not recommended to do this without backing everything up. Retry this command providing an additional argument --confirm to accept the risks and explicitly execute the downgrade."
# )
# exit(1)
#
# logger.info("Downgrading...")
# downgrade_database()
# exit()
intents = Intents.default()
intents.members = True
bot: PycordBot = PycordBot(scheduler=scheduler, intents=intents)
bot.load_extension("cogs")
try:
bot.run(config_get("bot_token", "bot"))
except LoginFailure as exc:
logger.error("Provided bot token is invalid: %s", exc)
except KeyboardInterrupt:
logger.info("KeyboardInterrupt received: Shutting down gracefully.")
except Exception as exc:
logger.error("An unexpected error has occurred: %s", exc, exc_info=exc)
exit(1)
finally:
exit()
if __name__ == "__main__":
main()