Discord/main.py

72 lines
1.8 KiB
Python
Raw Normal View History

2024-12-27 23:43:40 +02:00
import contextlib
2023-05-02 16:46:15 +03:00
import logging
import sys
2024-12-27 23:43:40 +02:00
from argparse import ArgumentParser
from logging import Logger
2024-12-17 23:14:06 +02:00
from pathlib import Path
2023-05-02 16:46:15 +03:00
2024-12-17 23:14:06 +02:00
from discord import LoginFailure, Intents
2024-12-26 20:12:50 +02:00
from libbot.utils import config_get
2023-05-02 16:46:15 +03:00
2024-12-17 23:14:06 +02:00
from classes.holo_bot import HoloBot
2024-12-27 23:43:40 +02:00
from modules.migrator import migrate_database
2024-12-16 21:59:10 +02:00
from modules.scheduler import scheduler
2023-05-02 16:46:15 +03:00
logging.basicConfig(
level=logging.INFO,
format="%(name)s.%(funcName)s | %(levelname)s | %(message)s",
datefmt="[%X]",
)
logger: Logger = logging.getLogger(__name__)
2023-05-02 16:46:15 +03:00
2024-12-27 23:43:40 +02:00
# Declare the parser that retrieves the command line arguments
parser = ArgumentParser(
prog="HoloUA Discord",
description="Discord bot for the HoloUA community.",
)
# Add a switch argument --migrate to be parsed...
parser.add_argument("--migrate", 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
2023-05-02 16:46:15 +03:00
uvloop.install()
2024-12-17 23:14:06 +02:00
def main() -> None:
if not Path("config.json").exists():
logger.error(
"Config file is missing: Make sure the configuration file 'config.json' is in place."
2023-05-06 20:06:04 +03:00
)
2024-12-17 23:14:06 +02:00
sys.exit()
2023-05-04 17:14:33 +03:00
2024-12-27 23:43:40 +02:00
# Perform migration if command line argument was provided
if args.migrate:
2024-12-28 00:00:27 +02:00
logger.info("Performing migrations...")
2024-12-27 23:43:40 +02:00
migrate_database()
2024-12-17 23:14:06 +02:00
intents: Intents = Intents().all()
client: HoloBot = HoloBot(intents=intents, scheduler=scheduler)
2023-05-02 16:46:15 +03:00
2024-06-23 13:05:03 +03:00
client.load_extension("cogs")
2023-05-04 17:09:47 +03:00
2023-05-02 16:46:15 +03:00
try:
2024-12-26 20:12:50 +02:00
client.run(config_get("bot_token", "bot"))
2024-12-17 23:14:06 +02:00
except LoginFailure as exc:
logger.error("Provided bot token is invalid: %s", exc)
2023-05-02 16:46:15 +03:00
except KeyboardInterrupt:
2024-12-17 23:14:06 +02:00
logger.info("KeyboardInterrupt received: Shutting down gracefully.")
finally:
sys.exit()
2023-05-02 16:46:15 +03:00
2023-05-04 17:14:33 +03:00
2023-05-02 16:46:15 +03:00
if __name__ == "__main__":
2023-05-04 17:14:33 +03:00
main()