From 0bf836eb705253e0cbc5cf878c78538932e73f97 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Thu, 29 Apr 2021 17:29:26 -0600 Subject: [PATCH] vc monitoring --- bot.js | 5 ++++- events/ready.js | 4 ++++ events/voiceStateUpdate.js | 17 ++++++++++++++ util/vcloop.js | 46 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 events/voiceStateUpdate.js create mode 100644 util/vcloop.js diff --git a/bot.js b/bot.js index 25181d7..9216ae2 100644 --- a/bot.js +++ b/bot.js @@ -24,7 +24,10 @@ client.misc = { hasLevelRoles: [] }, monit: {}, - monitEnabled: [] + monitEnabled: [], + inVC: [], + VCG: {}, + activeVC: [] }, loggers: {} }; diff --git a/events/ready.js b/events/ready.js index 10c1cdc..fa4bd72 100644 --- a/events/ready.js +++ b/events/ready.js @@ -7,10 +7,12 @@ const ora = require('ora'); const GuildSettings = require('../models/guild'); const BotDataSchema = require('../models/bot'); const LogData = require('../models/log'); +const Monitors = require('../models/monitor'); const siftStatuses = require('../util/siftstatuses'); const localXPCacheClean = require('../util/lxp/cacheloop'); const monitorCacheClean = require('../util/monitorloop'); +const vcloop = require('../util/vcloop'); let prefix = 'n?'; @@ -77,6 +79,8 @@ module.exports = async client => { setInterval(() => localXPCacheClean(client), 150000); setInterval(() => monitorCacheClean(client), 150000); + setInterval(() => vcloop(client), 60000); + let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) : new BotDataSchema({ diff --git a/events/voiceStateUpdate.js b/events/voiceStateUpdate.js new file mode 100644 index 0000000..dcfd41d --- /dev/null +++ b/events/voiceStateUpdate.js @@ -0,0 +1,17 @@ +const Monitor = require('../models/monitor'); + +export default async (client, oldState, voice) => { + if (client.users.cache.get(voice.member.id).bot) {return;} + if (voice.guild && client.misc.cache.monitEnabled.includes(voice.guild.id)) { + if (voice.channelID) { + client.misc.cache.VCG[voice.member.id] = voice.guild.id; + if (!client.misc.cache.inVC.includes(voice.member.id)) {client.misc.cache.inVC.push(voice.member.id);} + if (!client.misc.cache.activeVC.includes(voice.channelID)) {client.misc.cache.activeVC.push(voice.channelID);} + } else { + if (!client.misc.cache.inVC.includes(voice.member.id)) {return;} + client.misc.inVC.splice(client.misc.inVC.indexOf(voice.member.id), 1); + if (!voice.channel.members.size && client.misc.activeVC.includes(voice.channelID)) {client.misc.activeVC.splice(client.misc.activeVC.indexOf(voice.channelID), 1);} + if (Object.keys(client.misc.cache.VCG).includes(voice.member.id)) {delete client.misc.cache.VCG[voice.member.id];} + } + } +}; \ No newline at end of file diff --git a/util/vcloop.js b/util/vcloop.js new file mode 100644 index 0000000..7979a86 --- /dev/null +++ b/util/vcloop.js @@ -0,0 +1,46 @@ +module.exports = client => { + client.misc.cache.inVC.forEach(m => { + if (client.misc.cache.monitEnabled.includes(client.misc.cache.VCG[m])) { + if (!client.misc.cache.monit[client.misc.cache.VCG[m]]) { + let tm = await Monitors.findOne({gid: client.misc.cache.VCG[m]}); + client.misc.cache.monit[client.misc.cache.VCG[m]] = { + messages: tm.messages, + voice: tm.voice, + expiry: new Date() + }; + if (!tm) {return;} + } + if (!client.misc.cache.monit) {client.misc.cache.monit = {};} + if (!client.misc.cache.monit[client.misc.cache.VCG[m]].voice.members[m]) {client.misc.cache.monit[client.misc.cache.VCG[m]].voice.members[m] = 0;} + client.misc.cache.monit[client.misc.cache.VCG[m]].voice.members[m] += 1; + client.misc.cache.monit[client.misc.cache.VCG[m]].voice.total += 1; + client.misc.cache.monit[client.misc.cache.VCG[m]].expiry.setTime(Date.now()); + } else { + client.misc.cache.inVC.splice(client.misc.cache.inVC.indexOf(m), 1); + delete client.misc.cache.VCG[m]; + } + }); + + client.misc.cache.activeVC.forEach(vc => { + let g = client.guilds.cache.filter(g => g.channels.cache.has(vc)).first(); + if (!g) {return;} + g = g.id; + if (client.misc.cache.monitEnabled.includes(g)) { + if (!client.misc.cache.monit[g]) { + let tm = await Monitors.findOne({gid: g}); + client.misc.cache.monit[g] = { + messages: tm.messages, + voice: tm.voice, + expiry: new Date() + }; + if (!tm) {return;} + } + if (!client.misc.cache.monit) {client.misc.cache.monit = {};} + if (!client.misc.cache.monit[g].voice.channels[vc]) {client.misc.cache.monit[g].voice.channels[vc] = 0;} + client.misc.cache.monit[g].voice.channels[vc] += 1; + client.misc.cache.monit[g].expiry.setTime(Date.now()); + } else { + client.misc.cache.activeVC.splice(client.misc.cache.activeVC.indexOf(vc), 1); + } + }); +}; \ No newline at end of file