From 113ed159a05fcedf84415fbc7b7d8c2c11713858 Mon Sep 17 00:00:00 2001 From: fanyx Date: Fri, 13 Nov 2020 15:51:12 +0100 Subject: [PATCH] config building and ext loading --- main.py | 28 +++++++++++++++------------- src/commands/__init__.py | 0 src/utils/__init__.py | 1 + src/utils/config.py | 23 +++++++++++++++++++++++ 4 files changed, 39 insertions(+), 13 deletions(-) delete mode 100644 src/commands/__init__.py create mode 100644 src/utils/__init__.py create mode 100644 src/utils/config.py diff --git a/main.py b/main.py index 7f06489..484b7f0 100644 --- a/main.py +++ b/main.py @@ -1,21 +1,23 @@ -from configparser import ConfigParser import logging -from discord import Client import sys +from os import listdir -# read config at default location -# ./config.ini -config = ConfigParser() -try: - config.read_file(open("config.ini")) -except FileNotFoundError: - logging.critical("Unable to locate the configuration file.") - sys.exit(1) +from discord.ext.commands import Bot +from src.utils.config import build_config -# spawn discord client instance +# read config at configured location \\ default to 'config.ini' +config = build_config() + +# spawn discord bot instance # init token from config -bot = Client() +bot = Bot(command_prefix="~ak ") token = config['AUTH']['token'] +# load extensions +for file in os.listdir("src/cogs"): + if file.endswith(".py"): + name = file[:-3] + bot.load_extension(f"src.cogs.{name}") + if __name__ == "__main__": - bot.run() \ No newline at end of file + bot.run(token) \ No newline at end of file diff --git a/src/commands/__init__.py b/src/commands/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/utils/__init__.py @@ -0,0 +1 @@ + diff --git a/src/utils/config.py b/src/utils/config.py new file mode 100644 index 0000000..1803121 --- /dev/null +++ b/src/utils/config.py @@ -0,0 +1,23 @@ +import os +import logging + +from configparser import ConfigParser + + +def build_config(): + config_path = os.getenv("AKWAH_CONFIG_PATH") + if config_path == None: + config_path = "config.ini" + + try: + config_file = open(config_path) + except FileNotFoundError: + logging.critical("Unable to locate the configuration file.") + sys.exit(1) + + config = ConfigParser() + config.read_file(config_file) + logging.info("Finished building configuration.") + + return config +