mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2025-10-03 19:51:33 +01:00
34 lines
867 B
JavaScript
34 lines
867 B
JavaScript
const MusicSettings = require("../models/MusicSettings");
|
|
|
|
/** in-memory cache to cut Mongo roundtrips */
|
|
const cache = new Map(); // guildId -> settings doc (lean POJO)
|
|
|
|
async function ensure(guildId) {
|
|
if (!guildId) throw new Error("Missing guildId");
|
|
if (cache.has(guildId)) return cache.get(guildId);
|
|
|
|
let doc = await MusicSettings.findOne({ guildId }).lean();
|
|
if (!doc) {
|
|
doc = await MusicSettings.create({ guildId });
|
|
doc = doc.toObject();
|
|
}
|
|
cache.set(guildId, doc);
|
|
return doc;
|
|
}
|
|
|
|
async function set(guildId, patch) {
|
|
const updated = await MusicSettings.findOneAndUpdate(
|
|
{ guildId },
|
|
{ $set: patch },
|
|
{ upsert: true, new: true }
|
|
).lean();
|
|
cache.set(guildId, updated);
|
|
return updated;
|
|
}
|
|
|
|
function clear(guildId) {
|
|
if (guildId) cache.delete(guildId);
|
|
else cache.clear();
|
|
}
|
|
|
|
module.exports = { ensure, set, clear };
|