2024-08-10 09:44:22 +01:00
|
|
|
"""Very simple config loading library"""
|
2024-08-10 10:45:44 +01:00
|
|
|
from schema import Schema, And, Use
|
2024-08-10 10:58:31 +01:00
|
|
|
import files
|
2024-08-10 09:44:22 +01:00
|
|
|
|
2024-08-10 15:12:14 +01:00
|
|
|
_FILE = "config.json"
|
|
|
|
_SCHEMA = Schema(
|
|
|
|
{
|
|
|
|
# Discord bot token
|
|
|
|
"token": And(Use(str)),
|
|
|
|
|
|
|
|
# ids of owners authorised to use owner-only commands
|
|
|
|
"owners": And(Use(list[int])),
|
|
|
|
}
|
|
|
|
)
|
2024-08-10 09:44:22 +01:00
|
|
|
|
|
|
|
|
2024-08-10 10:45:44 +01:00
|
|
|
class Config():
|
|
|
|
def __init__(self, data: dict):
|
2024-08-10 15:12:14 +01:00
|
|
|
"""Initialise and validate the config"""
|
|
|
|
_SCHEMA.validate(data)
|
2024-08-10 10:45:44 +01:00
|
|
|
self.__dict__ = data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def token(self) -> str:
|
|
|
|
return self.__dict__["token"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def owners(self) -> list[int]:
|
|
|
|
return self.__dict__["owners"]
|
2024-08-10 15:12:14 +01:00
|
|
|
|
2024-08-10 10:55:09 +01:00
|
|
|
def reload(self) -> None:
|
|
|
|
"""Reload the config back into the dict"""
|
|
|
|
self.__dict__ = load().__dict__
|
2024-08-10 10:45:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load() -> Config:
|
2024-08-10 15:12:14 +01:00
|
|
|
"""Load the config"""
|
|
|
|
return Config(files.load(_FILE))
|