Discord/main.py

73 lines
1.9 KiB
Python
Raw Normal View History

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