view src/lib/Config.luan @ 23:e5c77f8d506d default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 15 Sep 2024 10:45:43 -0600
parents 80105b716a62
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local parse = Luan.parse or error()
local Db = require "site:/lib/Db.luan"


local Config = {}

local function get()
	local doc = Db.get_document("type:config")
	return doc and parse(doc.config)
end

function Config.get()
	return get() or error "config not set"
end

function Config.get_for_config()
	local config = get() or {}
	config.discord = config.discord or {}
	config.discord.client_id = config.discord.client_id or "replace"
	config.discord.client_secret = config.discord.client_secret or "replace"
	config.discord.bot_token = config.discord.bot_token or "replace"
	return config
end

function Config.set(text)
	parse(text)  -- validate
	Db.run_in_transaction( function()
		local doc = Db.get_document("type:config") or {type="config"}
		doc.config = text
		Db.save(doc)
	end )
end

return Config