-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.py
78 lines (56 loc) · 2.33 KB
/
db.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
74
75
76
77
78
import json
import logging
class BotDatabase:
def __init__(self):
self._data = {
"channels": {}
}
def load(self, path: str):
logging.info(f"Loading database from {path}")
with open(path, "r", encoding="utf-8") as db_file:
self._data = json.load(db_file)
def save(self, path: str):
logging.info(f"Saving database to {path}")
with open(path, "w", encoding="utf-8") as db_file:
json.dump(self._data, db_file)
@property
def data(self):
return self._data
@property
def channels(self):
return self._data["channels"]
def remove_channel(self, channel_id: int):
assert isinstance(channel_id, int)
channel_id = str(channel_id)
if channel_id in self._data["channels"]:
del self._data["channels"][channel_id]
return True
return False
def add_emote_filter(self, channel_id: int, regex: str):
assert isinstance(channel_id, int)
assert isinstance(regex, str)
channel_id = str(channel_id)
if channel_id not in self._data["channels"]:
self._data["channels"][channel_id] = {
"emote_filters": [],
"sent_emotes": [],
}
self._data["channels"][channel_id]["emote_filters"].append(regex)
def remove_emote_filter(self, channel_id: int, regex: str):
assert isinstance(channel_id, int)
assert isinstance(regex, str)
channel_id = str(channel_id)
if channel_id not in self._data["channels"]:
logging.warn(f"Channel with id {channel_id} not found")
return False
if regex not in self._data["channels"][channel_id]["emote_filters"]:
logging.warn(f"Emote filter for {regex} not found (filters: {self._data['channels'][channel_id]['emote_filters']})")
return False
self._data["channels"][channel_id]["emote_filters"].remove(regex)
return True
def get_emote_filters(self, channel_id: int):
assert isinstance(channel_id, int)
channel_id = str(channel_id)
if channel_id not in self._data["channels"]:
return []
return self._data["channels"][channel_id]["emote_filters"]