radical-bot/config.py
2022-08-19 02:17:34 -04:00

26 lines
760 B
Python

import json
from pathlib import Path
from dataclasses import dataclass
@dataclass
class Config(object):
discord_token: str | None = None
discord_guild_id: int | None = None
git_branch: str | None = None
def config_decoder(obj):
if '__type__' in obj and obj['__type__'] == "Config":
return Config(
discord_token=obj['discord_token'],
discord_guild_id=obj['discord_guild_id'],
git_branch=obj['git_branch']
)
def save(config: dict):
with Path("config.json").open("w+", encoding="utf-8") as fp:
json.dump(config, fp, indent=4)
def load():
with Path("config.json").open("r", encoding="utf-8") as fp:
config = json.load(fp, object_hook=config_decoder)
return config