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 10:55:09 +01:00
|
|
|
FILE = "config.json"
|
2024-08-10 09:44:22 +01:00
|
|
|
|
|
|
|
|
2024-08-10 10:45:44 +01:00
|
|
|
class Config():
|
|
|
|
def __init__(self, data: dict):
|
|
|
|
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 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:
|
|
|
|
"""Load the config and validate it"""
|
2024-08-10 10:58:31 +01:00
|
|
|
config = files.load(FILE)
|
2024-08-10 10:45:44 +01:00
|
|
|
Schema(
|
|
|
|
{
|
|
|
|
# Discord bot token
|
|
|
|
"token": And(Use(str)),
|
|
|
|
|
|
|
|
# ids of owners authorised to use owner-only commands
|
|
|
|
"owners": And(Use(list[int])),
|
|
|
|
}
|
|
|
|
).validate(config)
|
|
|
|
|
|
|
|
return Config(config)
|