3
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local parse = Luan.parse or error()
|
|
4 local Db = require "site:/lib/Db.luan"
|
|
5
|
|
6
|
|
7 local Config = {}
|
|
8
|
|
9 local function get()
|
|
10 local doc = Db.get_document("type:config")
|
|
11 return doc and parse(doc.config)
|
|
12 end
|
|
13
|
|
14 function Config.get()
|
|
15 return get() or error "config not set"
|
|
16 end
|
|
17
|
|
18 function Config.get_for_config()
|
|
19 local config = get() or {}
|
|
20 config.discord = config.discord or {}
|
|
21 config.discord.client_id = config.discord.client_id or "replace"
|
|
22 config.discord.client_secret = config.discord.client_secret or "replace"
|
|
23 return config
|
|
24 end
|
|
25
|
|
26 function Config.set(text)
|
|
27 parse(text) -- validate
|
|
28 Db.run_in_transaction( function()
|
|
29 local doc = Db.get_document("type:config") or {type="config"}
|
|
30 doc.config = text
|
|
31 Db.save(doc)
|
|
32 end )
|
|
33 end
|
|
34
|
|
35 return Config
|