|
|
@@ -5,113 +5,166 @@ from discord import Guild
|
|
5
|
5
|
|
|
6
|
6
|
from config import CONFIG
|
|
7
|
7
|
|
|
8
|
|
-class StateKey:
|
|
|
8
|
+class ConfigKey:
|
|
9
|
9
|
WARNING_CHANNEL_ID = 'warning_channel_id'
|
|
10
|
10
|
WARNING_MENTION = 'warning_mention'
|
|
11
|
11
|
|
|
12
|
12
|
class Storage:
|
|
13
|
13
|
"""
|
|
14
|
|
- Static class for managing persisted bot state.
|
|
|
14
|
+ Static class for managing persisted bot configuration and transient state
|
|
|
15
|
+ on a per-guild basis.
|
|
15
|
16
|
"""
|
|
16
|
17
|
|
|
17
|
|
- # discord.Guild.id -> dict
|
|
|
18
|
+ # -- Transient state management -----------------------------------------
|
|
|
19
|
+
|
|
18
|
20
|
__guild_id_to_state = {}
|
|
19
|
21
|
|
|
20
|
22
|
@classmethod
|
|
21
|
23
|
def get_state(cls, guild: Guild) -> dict:
|
|
22
|
24
|
"""
|
|
23
|
|
- Returns all persisted state for the given guild.
|
|
|
25
|
+ Returns transient state for the given guild. This state is not preserved
|
|
|
26
|
+ if the bot is restarted.
|
|
24
|
27
|
"""
|
|
25
|
28
|
state: dict = cls.__guild_id_to_state.get(guild.id)
|
|
26
|
|
- if state is not None:
|
|
27
|
|
- # Already in memory
|
|
28
|
|
- return state
|
|
29
|
|
- # Load from disk if possible
|
|
30
|
|
- cls.__trace(f'No loaded state for guild {guild.id}. Attempting to ' +
|
|
31
|
|
- 'load from disk.')
|
|
32
|
|
- state = cls.__load_guild_state(guild)
|
|
33
|
29
|
if state is None:
|
|
34
|
|
- return {}
|
|
35
|
|
- cls.__guild_id_to_state[guild.id] = state
|
|
|
30
|
+ state = {}
|
|
|
31
|
+ cls.__guild_id_to_state[guild.id] = state
|
|
36
|
32
|
return state
|
|
37
|
33
|
|
|
38
|
34
|
@classmethod
|
|
39
|
35
|
def get_state_value(cls, guild: Guild, key: str):
|
|
40
|
36
|
"""
|
|
41
|
|
- Returns a persisted state value stored under the given key. Returns
|
|
42
|
|
- None if not present.
|
|
|
37
|
+ Returns a state value for the given guild and key, or `None` if not set.
|
|
43
|
38
|
"""
|
|
44
|
39
|
return cls.get_state(guild).get(key)
|
|
45
|
40
|
|
|
46
|
41
|
@classmethod
|
|
47
|
42
|
def set_state_value(cls, guild: Guild, key: str, value) -> None:
|
|
48
|
43
|
"""
|
|
49
|
|
- Adds the given key-value pair to the persisted state for the given
|
|
50
|
|
- Guild. If `value` is `None` the key will be removed from persisted
|
|
51
|
|
- state.
|
|
|
44
|
+ Updates a transient value associated with the given guild and key name.
|
|
|
45
|
+ A value of `None` removes any previous value for that key.
|
|
52
|
46
|
"""
|
|
53
|
47
|
cls.set_state_values(guild, { key: value })
|
|
54
|
48
|
|
|
55
|
49
|
@classmethod
|
|
56
|
50
|
def set_state_values(cls, guild: Guild, vars: dict) -> None:
|
|
57
|
51
|
"""
|
|
58
|
|
- Merges the given `vars` dict with the saved state for the given guild
|
|
59
|
|
- and saves it to disk. `vars` must be JSON-encodable or a ValueError will
|
|
60
|
|
- be raised. Keys with associated values of `None` will be removed from the
|
|
61
|
|
- state.
|
|
|
52
|
+ Merges in a set of key-value pairs into the transient state for the
|
|
|
53
|
+ given guild. Any pairs with a value of `None` will be removed from the
|
|
|
54
|
+ transient state.
|
|
62
|
55
|
"""
|
|
63
|
56
|
if vars is None or len(vars) == 0:
|
|
64
|
57
|
return
|
|
65
|
58
|
state: dict = cls.get_state(guild)
|
|
|
59
|
+ for key, value in vars.items():
|
|
|
60
|
+ if value is None:
|
|
|
61
|
+ del state[key]
|
|
|
62
|
+ else:
|
|
|
63
|
+ state[key] = value
|
|
|
64
|
+ # XXX: Superstitious. Should update by ref already but saw weirdness once.
|
|
|
65
|
+ cls.__guild_id_to_state[guild.id] = state
|
|
|
66
|
+
|
|
|
67
|
+ # -- Persisted configuration management ---------------------------------
|
|
|
68
|
+
|
|
|
69
|
+ # discord.Guild.id -> dict
|
|
|
70
|
+ __guild_id_to_config = {}
|
|
|
71
|
+
|
|
|
72
|
+ @classmethod
|
|
|
73
|
+ def get_config(cls, guild: Guild) -> dict:
|
|
|
74
|
+ """
|
|
|
75
|
+ Returns all persisted configuration for the given guild.
|
|
|
76
|
+ """
|
|
|
77
|
+ config: dict = cls.__guild_id_to_config.get(guild.id)
|
|
|
78
|
+ if config is not None:
|
|
|
79
|
+ # Already in memory
|
|
|
80
|
+ return config
|
|
|
81
|
+ # Load from disk if possible
|
|
|
82
|
+ cls.__trace(f'No loaded config for guild {guild.id}. Attempting to ' +
|
|
|
83
|
+ 'load from disk.')
|
|
|
84
|
+ config = cls.__read_guild_config(guild)
|
|
|
85
|
+ if config is None:
|
|
|
86
|
+ return {}
|
|
|
87
|
+ cls.__guild_id_to_config[guild.id] = config
|
|
|
88
|
+ return config
|
|
|
89
|
+
|
|
|
90
|
+ @classmethod
|
|
|
91
|
+ def get_config_value(cls, guild: Guild, key: str):
|
|
|
92
|
+ """
|
|
|
93
|
+ Returns a persisted guild config value stored under the given key.
|
|
|
94
|
+ Returns `None` if not present.
|
|
|
95
|
+ """
|
|
|
96
|
+ return cls.get_config(guild).get(key)
|
|
|
97
|
+
|
|
|
98
|
+ @classmethod
|
|
|
99
|
+ def set_config_value(cls, guild: Guild, key: str, value) -> None:
|
|
|
100
|
+ """
|
|
|
101
|
+ Adds/updates the given key-value pair to the persisted config for the
|
|
|
102
|
+ given Guild. If `value` is `None` the key will be removed from persisted
|
|
|
103
|
+ config.
|
|
|
104
|
+ """
|
|
|
105
|
+ cls.set_config_values(guild, { key: value })
|
|
|
106
|
+
|
|
|
107
|
+ @classmethod
|
|
|
108
|
+ def set_config_values(cls, guild: Guild, vars: dict) -> None:
|
|
|
109
|
+ """
|
|
|
110
|
+ Merges the given `vars` dict with the saved config for the given guild
|
|
|
111
|
+ and writes it to disk. `vars` must be JSON-encodable or a `ValueError`
|
|
|
112
|
+ will be raised. Keys with associated values of `None` will be removed
|
|
|
113
|
+ from the persisted config.
|
|
|
114
|
+ """
|
|
|
115
|
+ if vars is None or len(vars) == 0:
|
|
|
116
|
+ return
|
|
|
117
|
+ config: dict = cls.get_config(guild)
|
|
66
|
118
|
try:
|
|
67
|
119
|
json.dumps(vars)
|
|
68
|
120
|
except:
|
|
69
|
121
|
raise ValueError(f'vars not JSON encodable - {vars}')
|
|
70
|
122
|
for key, value in vars.items():
|
|
71
|
123
|
if value is None:
|
|
72
|
|
- del state[key]
|
|
|
124
|
+ del config[key]
|
|
73
|
125
|
else:
|
|
74
|
|
- state[key] = value
|
|
75
|
|
- cls.__save_guild_state(guild, state)
|
|
|
126
|
+ config[key] = value
|
|
|
127
|
+ cls.__write_guild_config(guild, config)
|
|
76
|
128
|
|
|
77
|
129
|
@classmethod
|
|
78
|
|
- def __save_guild_state(cls, guild: Guild, state: dict) -> None:
|
|
|
130
|
+ def __write_guild_config(cls, guild: Guild, config: dict) -> None:
|
|
79
|
131
|
"""
|
|
80
|
|
- Saves state for a guild to a JSON file on disk.
|
|
|
132
|
+ Saves config for a guild to a JSON file on disk.
|
|
81
|
133
|
"""
|
|
82
|
|
- path: str = cls.__guild_path(guild)
|
|
83
|
|
- cls.__trace(f'Saving state for guild {guild.id} to {path}')
|
|
84
|
|
- cls.__trace(f'state = {state}')
|
|
|
134
|
+ path: str = cls.__guild_config_path(guild)
|
|
|
135
|
+ cls.__trace(f'Saving config for guild {guild.id} to {path}')
|
|
|
136
|
+ cls.__trace(f'config = {config}')
|
|
85
|
137
|
with open(path, 'w') as file:
|
|
86
|
138
|
# Pretty printing to make more legible for debugging
|
|
87
|
139
|
# Sorting keys to help with diffs
|
|
88
|
|
- json.dump(state, file, indent='\t', sort_keys=True)
|
|
|
140
|
+ json.dump(config, file, indent='\t', sort_keys=True)
|
|
89
|
141
|
cls.__trace('State saved')
|
|
90
|
142
|
|
|
91
|
143
|
@classmethod
|
|
92
|
|
- def __load_guild_state(cls, guild: Guild) -> dict:
|
|
|
144
|
+ def __read_guild_config(cls, guild: Guild) -> dict:
|
|
93
|
145
|
"""
|
|
94
|
|
- Loads state for a guild from a JSON file on disk, or None if not found.
|
|
|
146
|
+ Loads config for a guild from a JSON file on disk, or `None` if not
|
|
|
147
|
+ found.
|
|
95
|
148
|
"""
|
|
96
|
|
- path: str = cls.__guild_path(guild)
|
|
|
149
|
+ path: str = cls.__guild_config_path(guild)
|
|
97
|
150
|
if not exists(path):
|
|
98
|
|
- cls.__trace(f'No state on disk for guild {guild.id}. Returning None.')
|
|
|
151
|
+ cls.__trace(f'No config on disk for guild {guild.id}. Returning None.')
|
|
99
|
152
|
return None
|
|
100
|
|
- cls.__trace(f'Loading state from disk for guild {guild.id}')
|
|
|
153
|
+ cls.__trace(f'Loading config from disk for guild {guild.id}')
|
|
101
|
154
|
with open(path, 'r') as file:
|
|
102
|
|
- state = json.load(file)
|
|
|
155
|
+ config = json.load(file)
|
|
103
|
156
|
cls.__trace('State loaded')
|
|
104
|
|
- return state
|
|
|
157
|
+ return config
|
|
105
|
158
|
|
|
106
|
159
|
@classmethod
|
|
107
|
|
- def __guild_path(cls, guild: Guild) -> str:
|
|
|
160
|
+ def __guild_config_path(cls, guild: Guild) -> str:
|
|
108
|
161
|
"""
|
|
109
|
|
- Returns the JSON file path where guild state should be written.
|
|
|
162
|
+ Returns the JSON file path where guild config should be written.
|
|
110
|
163
|
"""
|
|
111
|
|
- config_value: str = CONFIG['statePath']
|
|
|
164
|
+ config_value: str = CONFIG['configPath']
|
|
112
|
165
|
path: str = config_value if config_value.endswith('/') else f'{config_value}/'
|
|
113
|
166
|
return f'{path}guild_{guild.id}.json'
|
|
114
|
167
|
|
|
115
|
168
|
@classmethod
|
|
116
|
169
|
def __trace(cls, message: str) -> None:
|
|
117
|
|
- print(f'{Storage.__name__}: {message}')
|
|
|
170
|
+ print(f'{cls.__name__}: {message}')
|