From b71dc7b24d802178dcc8db4cd52a3a7a291689a9 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sat, 20 Feb 2021 20:43:34 -0700 Subject: [PATCH] Cache cleaning/expiry and db saving --- .../{setupeconomy.js => setupleveling.js} | 2 ++ events/ready.js | 8 +++++--- util/lxp/cacheloop.js | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) rename commands/{setupeconomy.js => setupleveling.js} (97%) create mode 100644 util/lxp/cacheloop.js diff --git a/commands/setupeconomy.js b/commands/setupleveling.js similarity index 97% rename from commands/setupeconomy.js rename to commands/setupleveling.js index 74e20b2..184327a 100644 --- a/commands/setupeconomy.js +++ b/commands/setupleveling.js @@ -31,6 +31,8 @@ module.exports = { rc.on("collect", async r => { let xp = new LXP({gid: message.guild.id}); xp.msg = r.emoji.name === "👍"; + xp.save(); + client.misc.cache.lxp.enabled.push(message.guild.id); return message.channel.send(new Discord.MessageEmbed() .setTitle("XP System Enabled!") .setThumbnail(message.guild.iconURL({size: 2048})) diff --git a/events/ready.js b/events/ready.js index 2c63316..8e6ff3f 100644 --- a/events/ready.js +++ b/events/ready.js @@ -9,8 +9,9 @@ const BotDataSchema = require('../models/bot'); const LogData = require('../models/log'); const siftStatuses = require('../util/siftstatuses'); +const localXPCacheClean = require('../util/lxp/cacheloop'); -var prefix = 'n?'; +let prefix = 'n?'; module.exports = async client => { const config = client.config; @@ -51,7 +52,6 @@ module.exports = async client => { else {client.user.setActivity(responses[type][Math.floor(Math.random() * responses[type].length)] + " | " + prefix + "help", {type: type});} } setR(); - setInterval(setR, 14400000); const setPL = async () => {let tg; for (tg of Array.from(client.guilds.cache.values)) { @@ -67,12 +67,14 @@ module.exports = async client => { } }}; setPL(); - siftStatuses(); + siftStatuses(); setInterval(() => {setPL(); siftStatuses(client, null);}, 120000); await require('../util/cache')(client); + setInterval(() => localXPCacheClean(client), 150000); + let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) : new BotDataSchema({ diff --git a/util/lxp/cacheloop.js b/util/lxp/cacheloop.js new file mode 100644 index 0000000..4c2ac14 --- /dev/null +++ b/util/lxp/cacheloop.js @@ -0,0 +1,19 @@ +const LXP = require('../../models/localxp'); + +module.exports = async (client) => { + let cd = new Date().getTime(); + await client.misc.cache.lxp.enabled.forEach(gxp => { + LXP.findOne({gid: gxp}).then(xp => { + if (!xp) {return;} + Object.keys(client.misc.cache.lxp.xp[gxp]).forEach(user => { + xp.xp[user] = [client.misc.cache.lxp.xp[gxp][user].xp, client.misc.cache.lxp.xp[gxp][user].level]; + xp.markModified(`xp.${user}`); + xp.save(); + if (cd - client.misc.cache.lxp.xp[gxp][user].lastXP > 600000) { + delete client.misc.cache.lxp.xp[gxp][user]; + if (!Object.keys(client.misc.cache.lxp.xp[gxp]).length) {delete client.misc.cache.lxp.xp[gxp];} + } + }); + }) + }) +}; \ No newline at end of file