Bot pro KSP Discord
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

from typing import Any
import json
import os.path
from hrochobot.utils.json_templates import TEMPLATES
EXAMPLE_DATA = "data.example"
DATA_FOLDER = "TODO"
def load_json(filename: str):
filename += ".json"
if not os.path.exists(filename):
return json.loads(TEMPLATES[os.path.basename(filename)])
with open(filename) as f:
content = json.load(f)
return content
def dump_json(filename: str, data: Any):
filename += ".json"
path = os.path.dirname(filename)
if not os.path.exists(path):
os.makedirs(path)
with open(filename, "w") as f:
json.dump(data, f)
def load_data(filename: str):
return load_json(os.path.join(DATA_FOLDER, filename))
def dump_data(filename: str, data: Any):
return dump_json(os.path.join(DATA_FOLDER, filename), data)
def load_guild_data(guild: int, filename: str):
return load_data(os.path.join(str(guild), filename))
def dump_guild_data(guild: int, filename: str, data: Any):
return dump_data(os.path.join(str(guild), filename), data)