-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbutils.py
44 lines (32 loc) · 1.1 KB
/
dbutils.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
import os
import discord
import json
DB_NAME = 'storage/db.json'
def load_db(guild: discord.Guild) -> dict:
# return this guild's entry in db.json
db = _load_db()
_validate_db(guild, db)
return db[str(guild.id)]
def save_db(guild: discord.Guild, data: dict) -> None:
current_db = _load_db()
with open(DB_NAME, 'w') as db:
current_db[str(guild.id)] = data
json.dump(current_db, db)
def _load_db():
# load the entire db.json
if not os.path.isfile(DB_NAME):
with open(DB_NAME, 'w') as db:
db.write(json.dumps({}))
with open(DB_NAME, 'r') as db:
return json.load(db)
def _validate_db(guild: discord.Guild, db: dict) -> None:
# Validates db schema, repairing if necessary
dirty = False
if str(guild.id) not in db or 'enabled_channel' not in db[str(guild.id)]:
db[str(guild.id)] = {'enabled_channel': -1}
dirty = True
elif not isinstance(db[str(guild.id)]['enabled_channel'], int):
db[str(guild.id)]['enabled_channel'] = -1
dirty = True
if dirty:
save_db(guild, db[str(guild.id)])