From 57282d9c56ea4ea6cfb5a2b7691802e16badf9a9 Mon Sep 17 00:00:00 2001 From: Cromatin Date: Fri, 11 Oct 2024 18:32:21 +0200 Subject: [PATCH] Implemented basic functionality to bring the bot online --- .gitignore | 1 + config/.bot.template | 2 ++ src/main.py | 51 +++++++++++++++++++++++++++++++++++++ src/utils/datetime_tools.py | 7 ++++- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 config/.bot.template diff --git a/.gitignore b/.gitignore index b37e465..6db07a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Custom additions logs/* config/*.ini +pastebin.txt # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/config/.bot.template b/config/.bot.template new file mode 100644 index 0000000..8ff3390 --- /dev/null +++ b/config/.bot.template @@ -0,0 +1,2 @@ +[DISCORD] +TOKEN = \ No newline at end of file diff --git a/src/main.py b/src/main.py index e69de29..6b1db6e 100644 --- a/src/main.py +++ b/src/main.py @@ -0,0 +1,51 @@ +from datetime import datetime +startup = datetime.now().timestamp() + +# Initialize the logger +from utils.logger.custom_logging import Custom_Logger +Custom_Logger.initialize() + +import logging +app_logger = logging.getLogger("app") + +from utils.adv_configparser import Advanced_ConfigParser +from utils.datetime_tools import get_elapsed_time_smal, get_elapsed_time_big +import discord +from discord.ext import commands +from pathlib import Path +import re + +source_path = Path(__file__).resolve() +base_path = source_path.parents[1] +app_logger.info(f"Using the following path as entrypoint: '{base_path}'") + +intents = discord.Intents.default() +intents.messages = True + +class MyBot(commands.Bot): + def __init__(self): + super().__init__(command_prefix=None, help_command=None, intents=intents) + +bot = MyBot() +bot_config = Advanced_ConfigParser(Path.joinpath(base_path, "config", "bot.ini")) +if re.match(r'[A-Za-z\d]{24}\.[\w-]{6}\.[\w-]{27}', bot_config["DISCORD"]["TOKEN"]): + app_logger.critical("Bot (config/bot.ini) configuration invalid, please set a valid token") + quit(1) +elif bot_config.compare_to_template() not in ("equal", "config_minus"): + app_logger.critical("Bot (config/bot.ini) configuration is missing some parts. Make sure it at least has all the same keys as the template") + quit(1) +else: + app_logger.info("Bot configuration valid, continuing with startup") + +@bot.event +async def on_ready(): + app_logger.info(f"Successfully logged in (after {get_elapsed_time_smal(datetime.now().timestamp() - startup)}) as {bot.user}") + +try: + bot.run(bot_config["DISCORD"]["TOKEN"], log_handler = None) +except discord.errors.LoginFailure: + app_logger.critical("Improper token has been passed. Aborting startup") + quit(1) + +app_logger.info("Quitting application ...") +app_logger.info(f"Exiting. Application ran for {get_elapsed_time_big(datetime.now().timestamp() - startup)}") \ No newline at end of file diff --git a/src/utils/datetime_tools.py b/src/utils/datetime_tools.py index 9afba1e..c1f1f82 100644 --- a/src/utils/datetime_tools.py +++ b/src/utils/datetime_tools.py @@ -13,4 +13,9 @@ def get_elapsed_time_ms(timestamp:float) -> str: def get_elapsed_time_smal(timestamp:float) -> str: """Convert an timestamp into an predefined elapsed time format (00sec 000ms)""" time_elapsed = datetime.fromtimestamp(timestamp) - return f"{time_elapsed.second:02}sec {time_elapsed.microsecond // 1000:03}ms" \ No newline at end of file + return f"{time_elapsed.second:02}sec {time_elapsed.microsecond // 1000:03}ms" + +def get_elapsed_time_big(timestamp:float) -> str: + """Convert an timestamp into an predefined elapsed time format (00sec 000ms)""" + time_elapsed = datetime.fromtimestamp(timestamp) + return f"{time_elapsed.day - 1}days {time_elapsed.hour - 1}hrs {1 if time_elapsed.minute == 0 else time_elapsed.minute}min" \ No newline at end of file