make monitors work

master
Kit Kasune 3 years ago
parent 64057c3332
commit 63b21837d0
  1. 3
      bot.js
  2. 3
      commands/utility/monitor.js
  3. 18
      events/message.js
  4. 4
      util/cache/monit.js
  5. 20
      util/monitorloop.js

@ -23,7 +23,8 @@ client.misc = {
xp: {},
hasLevelRoles: []
},
monit: {}
monit: {},
monitEnabled: []
},
loggers: {}
};

@ -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!");
}

@ -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 {

@ -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);
}
}

@ -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];}
});
});
};
Loading…
Cancel
Save