34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
import { getDatabase } from '../index.js';
|
|
|
|
export function getSetting(guildId, key, fallback = null) {
|
|
const row = getDatabase()
|
|
.prepare('SELECT value FROM guild_settings WHERE guild_id = ? AND key = ?')
|
|
.get(guildId, key);
|
|
return row ? JSON.parse(row.value) : fallback;
|
|
}
|
|
|
|
/**
|
|
* Reads a setting without throwing when the database is not ready (tests, early startup).
|
|
* Never use it when a missing database should be an error.
|
|
*/
|
|
export function resolveSetting(guildId, key, fallback = null) {
|
|
try {
|
|
return getSetting(guildId, key, fallback);
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function setSetting(guildId, key, value, updatedBy = null) {
|
|
getDatabase()
|
|
.prepare(
|
|
`INSERT INTO guild_settings (guild_id, key, value, updated_by)
|
|
VALUES (?, ?, ?, ?)
|
|
ON CONFLICT (guild_id, key) DO UPDATE SET
|
|
value = excluded.value,
|
|
updated_by = excluded.updated_by,
|
|
updated_at = datetime('now')`,
|
|
)
|
|
.run(guildId, key, JSON.stringify(value), updatedBy);
|
|
}
|