diff --git a/commands/utility/monitor.js b/commands/utility/monitor.js index 7c33866..33d21aa 100644 --- a/commands/utility/monitor.js +++ b/commands/utility/monitor.js @@ -3,6 +3,8 @@ const Discord = require('discord.js'); const GuildData = require('../../models/guild'); const Monitors = require('../../models/monitor'); +const ask = require('../../util/ask'); + module.exports = { name: "monitor", aliases: ['serverstats', 'monit'], @@ -21,10 +23,30 @@ module.exports = { let tm = await Monitors.findOne({gid: message.guild.id}); if (!args.length || (args.length && ['v', 'view', 'stats'].includes(args[0].toLowerCase()))) { + if (!tm) {return message.channel.send("Your server doesn't have monitoring enabled. If it's something you actually think you'll use, feel free to run `setup` on this command to enable it!");} + + let ch = Object.keys(tm.messages.channels).sort((b, a) => {tm.messages.channels[a] - tm.messages.channels[b];}).slice(0, Object.keys(tm.messages.channels).length >= 5 ? 5 : Object.keys(tm.messages.channels).length); + let chs = ``; + let i; for (i=0; i -> **${tm.messages.channels[ch[i]]} Messages**\n`;} + + let u = Object.keys(tm.messages.members).sort((b, a) => {tm.messages.members[a] - tm.messages.members[b];}).slice(0, Object.keys(tm.messages.members).length >= 5 ? 5 : Object.keys(tm.messages.members).length); + let us = ``; + let i2; for (i2=0; i2 -> **${tm.messages.members[u[i2]]} Messages**\n`;} + return message.channel.send(new Discord.MessageEmbed() + .setTitle(`Activity stats for ${message.guild.name}`) + .setThumbnail(message.guild.iconURL({size: 1024})) + .setDescription("*These statistics have an accuracy of about 10 minutes.*") + .addField("Top Channels", chs) + .addField("Top Members", us) + .addField("Total Messages in Server", tm.messages.total) + .setColor('c375f0') + .setFooter("Natsuki", client.user.avatarURL()) + .setTimestamp() + ); } - if (['s', 'setup', 'config', 'configure'].includes(args[0].toLowerCase())) { + if (['s', 'setup', 'config', 'configure', 'enable', 'e'].includes(args[0].toLowerCase())) { let tg = await GuildData.findOne({gid: message.guild.id}); if ((tg && tg.staffrole && tg.staffrole.length && !message.member.roles.cache.has(tg.staffrole)) && !message.member.permissions.has("ADMINISTRATOR")) {return message.channel.send("You must be an administrator or have the staff role in your server in order to use this command!");} if (tm) {return message.channel.send("You already have an activity monitor set up!");} @@ -35,6 +57,15 @@ module.exports = { return message.channel.send("Your server activity monitor has been set up successfully!"); } + if (['d', 'disable'].includes(args[0].toLowerCase())) { + let conf = await ask(message, "Are you sure you want to disable monitors for this server? This will delete all monitoring data.", 60000); + if (!conf || !['yes', 'y'].includes(conf.toLowerCase().trim())) {return message.channel.send("Okay. I won't delete or disable anything.");} + delete client.misc.cache.monit[message.guild.id]; + client.misc.cache.monitEnabled.splice(client.misc.cache.monitEnabled.indexOf(message.guild.id), 1); + await Monitors.deleteOne({gid: message.guild.id}); + return message.channel.send("Monitors for this server have been disabled."); + } + else {return message.channel.send(`Syntax: \`${prefix}monitor \``);} } }; \ No newline at end of file diff --git a/events/message.js b/events/message.js index 5e05e9c..2ea41cc 100644 --- a/events/message.js +++ b/events/message.js @@ -75,6 +75,7 @@ module.exports = async (client, message) => { expiry: new Date() }; } + if (!client.misc.cache.monit) {client.misc.cache.monit = {};} 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; diff --git a/events/ready.js b/events/ready.js index 8e6ff3f..10c1cdc 100644 --- a/events/ready.js +++ b/events/ready.js @@ -10,6 +10,7 @@ const LogData = require('../models/log'); const siftStatuses = require('../util/siftstatuses'); const localXPCacheClean = require('../util/lxp/cacheloop'); +const monitorCacheClean = require('../util/monitorloop'); let prefix = 'n?'; @@ -74,6 +75,7 @@ module.exports = async client => { await require('../util/cache')(client); setInterval(() => localXPCacheClean(client), 150000); + setInterval(() => monitorCacheClean(client), 150000); let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) diff --git a/util/cache.js b/util/cache.js index 536c310..b22abd6 100644 --- a/util/cache.js +++ b/util/cache.js @@ -24,7 +24,7 @@ module.exports = async (client) => { ora_lrCache.stop(); ora_lrCache.clear(); console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.hasLevelRoles.length}`)} ${chalk.blueBright(`guilds with Level Roles enabled.`)}`); - let ora_lrCache = ora("Caching Monitors...").start(); + let ora_moCache = ora("Caching Monitors...").start(); await require('./cache/monit')(client); ora_moCache.stop(); ora_moCache.clear(); console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${Object.keys(client.misc.cache.monit).length}`)} ${chalk.blueBright(`guilds with Monitors enabled.`)}`); diff --git a/util/cache/monit.js b/util/cache/monit.js index ddfd05d..3c845e3 100644 --- a/util/cache/monit.js +++ b/util/cache/monit.js @@ -2,6 +2,7 @@ const Monitor = require('../../models/monitor'); module.exports = async client => { client.misc.cache.monit = {}; + client.misc.cache.monitEnabled = []; for await (const tm of Monitor.find()) { client.misc.cache.monit[tm.gid] = {