config building and ext loading

This commit is contained in:
fanyx 2020-11-13 15:51:12 +01:00
parent d9aa1df130
commit 113ed159a0
4 changed files with 39 additions and 13 deletions

28
main.py
View File

@ -1,21 +1,23 @@
from configparser import ConfigParser
import logging import logging
from discord import Client
import sys import sys
from os import listdir
# read config at default location from discord.ext.commands import Bot
# ./config.ini from src.utils.config import build_config
config = ConfigParser()
try:
config.read_file(open("config.ini"))
except FileNotFoundError:
logging.critical("Unable to locate the configuration file.")
sys.exit(1)
# spawn discord client instance # read config at configured location \\ default to 'config.ini'
config = build_config()
# spawn discord bot instance
# init token from config # init token from config
bot = Client() bot = Bot(command_prefix="~ak ")
token = config['AUTH']['token'] 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__": if __name__ == "__main__":
bot.run() bot.run(token)

1
src/utils/__init__.py Normal file
View File

@ -0,0 +1 @@

23
src/utils/config.py Normal file
View File

@ -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