discordBot/scr/config.py

101 lines
2.0 KiB
Python

from __future__ import annotations
import json
from dataclasses import dataclass
@dataclass
class MysqlSettings:
user: str
password: str
host: str
port: int
db: str
@dataclass
class BotSettings:
token: str
guildId: int
prefix: str
@dataclass
class RoleSettings:
member: str
roles: BotSettings.RoleSettings
@dataclass
class ChannelSettings:
joinLog: int
leaveLog: int
channels: BotSettings.ChannelSettings
class CommandSettings:
def load(self, data):
pass
commands: dict[str, CommandSettings]
@dataclass
class YoutrackSettings:
url: str
class Config:
def __init__(self, configFile: str = "./settings.json"):
self.configFile = configFile
with open(self.configFile, 'r') as file:
self.configData = json.loads(file.read())
self.__load_mysql()
self.__load_bot()
self.__load_youtrack()
def register_command_settings(self, settingJson: str):
def decorator(cls: type[BotSettings.CommandSettings]):
currentSettings = self.BOT.commands.get(settingJson)
if currentSettings is not None:
raise Exception(f"Setting {settingJson} already loaded.")
instance = cls()
instance.load(self.commandData[settingJson])
self.BOT.commands[settingJson] = instance
return decorator
def __load_mysql(self):
mysqlData = self.configData["mysql"]
self.MYSQL = MysqlSettings(
user=mysqlData["user"],
password=mysqlData["password"],
host=mysqlData["host"],
port=mysqlData["port"],
db=mysqlData["db"]
)
def __load_bot(self):
botData = self.configData["bot"]
self.BOT = BotSettings(
token=botData["token"],
guildId=botData["guild"],
prefix=botData["prefix"],
roles=BotSettings.RoleSettings(
member=botData["roles"]["member"]
),
channels=BotSettings.ChannelSettings(
joinLog=botData["channels"]["joinLog"],
leaveLog=botData["channels"]["leaveLog"]
),
commands={}
)
self.commandData = botData["commands"]
def __load_youtrack(self):
youtrackData = self.configData["youtrack"]
self.YOUTRACK = YoutrackSettings(
url=youtrackData["url"]
)