Pull out the config and history classes

This commit is contained in:
Marc Di Luzio 2024-08-10 10:45:44 +01:00
parent d0865bd780
commit c44f16dd8f
3 changed files with 96 additions and 55 deletions

View file

@ -1,9 +1,34 @@
"""Very simple config loading library"""
import json
from schema import Schema, And, Use
import matching
CONFIG = "config.json"
def load() -> dict:
with open(CONFIG) as f:
return json.load(f)
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"]
def load() -> Config:
"""Load the config and validate it"""
config = matching.load(CONFIG)
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)