forked from ManicJamie/HornetBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.py
73 lines (61 loc) · 2.26 KB
/
save.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json, os, shutil
JSON_PATH = "save.json"
data : dict = {}
VERSION = 0.1
def save():
shutil.copy2(JSON_PATH, JSON_PATH + ".bak")
# For write-time safety - if we error mid-write then contents of the file won't be completed!
with open(JSON_PATH, "w") as f:
json.dump(data, f, indent=4)
if not os.path.exists(JSON_PATH):
data = {"version": VERSION, "module_templates": {}, "guilds": {}}
save()
else:
with open(JSON_PATH) as f:
try:
data = json.load(f)
except json.JSONDecodeError:
print("Could not deserialise save.json - loading backup")
with open(JSON_PATH + ".bak") as f2:
data = json.load(f2)
if VERSION > data["version"]:
print(f"Save json is out of date! Json ver: {data['version']} < Save ver: {VERSION}")
exit(11)
def reload():
"""Method to serialise and deserialise data from a json. Used to dereference module dictionaries"""
global data
save()
with open(JSON_PATH) as f:
data = json.load(f)
def addModuleTemplate(module_name, init_data):
data["module_templates"][module_name] = init_data
save()
def initModules():
for guild_id in getGuildIds():
modules = getGuildData(guild_id)["modules"]
for templ_name in data["module_templates"]:
if templ_name not in modules:
modules[templ_name] = data["module_templates"][templ_name]
reload()
def getGuildIds() -> list[str]:
return data["guilds"].keys()
def getModuleData(guild_id, module_name):
return getGuildData(guild_id)["modules"][module_name]
def getGuildData(guild_id):
if str(guild_id) not in data["guilds"].keys():
initGuildData(guild_id)
return data["guilds"][str(guild_id)]
def initGuildData(guild_id : str, guild_name : str = ""):
data["guilds"][guild_id] = {
"nick" : guild_name,
"adminRoles" : [],
"spoileredPlayers": [],
"modules" : {module:default for module, default in list(data["module_templates"].items())}
}
reload()
def initModule(module_name, init_data):
for guild_id in getGuildIds():
modules = getGuildData(guild_id)["modules"]
if module_name not in modules:
modules[module_name] = init_data
save()