From 63b21837d0b58fb7c31cfffc4a93de723efe6fb3 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Tue, 27 Apr 2021 22:07:04 -0600 Subject: [PATCH] make monitors work --- bot.js | 3 ++- commands/utility/monitor.js | 3 ++- events/message.js | 18 ++++++++++++++++++ util/cache/monit.js | 4 +++- util/monitorloop.js | 20 ++++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 util/monitorloop.js diff --git a/bot.js b/bot.js index 6b0be67..25181d7 100644 --- a/bot.js +++ b/bot.js @@ -23,7 +23,8 @@ client.misc = { xp: {}, hasLevelRoles: [] }, - monit: {} + monit: {}, + monitEnabled: [] }, loggers: {} }; diff --git a/commands/utility/monitor.js b/commands/utility/monitor.js index 4575b34..7c33866 100644 --- a/commands/utility/monitor.js +++ b/commands/utility/monitor.js @@ -30,7 +30,8 @@ module.exports = { if (tm) {return message.channel.send("You already have an activity monitor set up!");} tm = new Monitors({gid: message.guild.id}); tm.save(); - client.misc.cache.monit[message.guild.id] = {messages: {channels: {}, members: {}}, voice: {channels: {}, members: {}}}; + client.misc.cache.monit[message.guild.id] = {messages: {channels: {}, members: {}, total: 0}, voice: {channels: {}, members: {}, total: 0}, expiry: new Date()}; + client.misc.cache.monitEnabled.push(message.guild.id); return message.channel.send("Your server activity monitor has been set up successfully!"); } diff --git a/events/message.js b/events/message.js index 85882fe..5e05e9c 100644 --- a/events/message.js +++ b/events/message.js @@ -6,6 +6,7 @@ const wait = require('../util/wait'); const UserData = require('../models/user'); const AR = require('../models/ar'); const LXP = require('../models/localxp'); +const Monitors = require('../models/monitor'); module.exports = async (client, message) => { if (message.author.bot) {return undefined;} @@ -65,6 +66,23 @@ module.exports = async (client, message) => { }); } + if (message.guild && client.misc.cache.monitEnabled.includes(message.guild.id)) { + if (!client.misc.cache.monit[message.guild.id]) { + let tm = await Monitors.findOne({gid: message.guild.id}); + client.misc.cache.monit[tm.gid] = { + messages: tm.messages, + voice: tm.voice, + expiry: new Date() + }; + } + if (!client.misc.cache.monit[message.guild.id].messages.channels[message.channel.id]) {client.misc.cache.monit[message.guild.id].messages.channels[message.channel.id] = 0;} + if (!client.misc.cache.monit[message.guild.id].messages.members[message.author.id]) {client.misc.cache.monit[message.guild.id].messages.members[message.author.id] = 0;} + client.misc.cache.monit[message.guild.id].messages.channels[message.channel.id] += 1; + client.misc.cache.monit[message.guild.id].messages.members[message.author.id] += 1; + client.misc.cache.monit[message.guild.id].messages.total += 1; + client.misc.cache.monit[message.guild.id].expiry.setTime(Date.now()); + } + try { diff --git a/util/cache/monit.js b/util/cache/monit.js index 8610de1..ddfd05d 100644 --- a/util/cache/monit.js +++ b/util/cache/monit.js @@ -6,7 +6,9 @@ module.exports = async client => { for await (const tm of Monitor.find()) { client.misc.cache.monit[tm.gid] = { messages: tm.messages, - voice: tm.voice + voice: tm.voice, + expiry: new Date() }; + client.misc.cache.monitEnabled.push(tm.gid); } } \ No newline at end of file diff --git a/util/monitorloop.js b/util/monitorloop.js new file mode 100644 index 0000000..0fac2e1 --- /dev/null +++ b/util/monitorloop.js @@ -0,0 +1,20 @@ +const Monitors = require('../models/monitor'); + +module.exports = async (client) => { + let cd = new Date().getTime(); + Object.keys(client.misc.cache.monit).forEach(cache => { + Monitors.findOne({gid: cache}).then(tm => { + if (!tm) {return;} + tm.messages = client.misc.cache.monit[cache].messages; + tm.markModified(`messages.total`); + Object.keys(client.misc.cache.monit[cache].messages.members).forEach(m => tm.markModified(`messages.members.${m}`)); + Object.keys(client.misc.cache.monit[cache].messages.channels).forEach(c => tm.markModified(`messages.channels.${c}`)); + tm.voice = client.misc.cache.monit[cache].voice; + tm.markModified(`voice.total`); + Object.keys(client.misc.cache.monit[cache].voice.members).forEach(m => tm.markModified(`voice.members.${m}`)); + Object.keys(client.misc.cache.monit[cache].voice.channels).forEach(c => tm.markModified(`voice.channels.${c}`)); + tm.save(); + if (cd > client.misc.cache.monit[cache].expiry.getTime()) {delete client.misc.cache.monit[cache];} + }); + }); +}; \ No newline at end of file