From 23d63a86e8a346e1cf32a6ac96e5b0703ca419cf Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 15 Jul 2022 11:50:32 +0300 Subject: [PATCH] modstats command --- .../commands/administration/Modstats.js | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/structure/components/commands/administration/Modstats.js diff --git a/src/structure/components/commands/administration/Modstats.js b/src/structure/components/commands/administration/Modstats.js new file mode 100644 index 0000000..bfc7d49 --- /dev/null +++ b/src/structure/components/commands/administration/Modstats.js @@ -0,0 +1,83 @@ +const { MessageAttachment } = require("discord.js"); +const { SlashCommand } = require("../../../interfaces"); + +class ModstatsCommand extends SlashCommand { + + constructor(client) { + super(client, { + name: 'modstats', + description: 'Aggregate moderation statistics', + module: 'administration', + guildOnly: true, + memberPermissions: ['MANAGE_GUILD'], + clientPermissions: ['ATTACH_FILES'], + options: [ + { + name: ['after', 'before'], + description: ['Count actions AFTER this date', 'Count actions BEFORE this date, can also be "now"'], + type: 'DATE', + dependsOn: ['before', 'after'] + } + ] + }); + } + + async execute(invoker, opts) { + + const now = Date.now(); + const { guild } = invoker; + const query = { guild: guild.id }; + const result = { + totals: {}, + time: { + gathered: now, + rangeStart: null, + rangeEnd: now + } }; + + if (opts.after && opts.before) { + query.timestamp = { + $gt: opts.after.valueOf(), + $lt: opts.before.valueOf() + }; + } + + const data = await this.client.mongodb.infractions.find(query, { projection: { executor: 1, type: 1 } }); + for (const log of data) { + if (log.executor === guild.me.id) continue; + if (!result[log.executor]) { + const user = await this.client.resolveUser(log.executor); + result[log.executor] = { name: user.tag }; + } + if (!result[log.executor][log.type]) result[log.executor][log.type] = 0; + result[log.executor][log.type]++; + } + + for (const user of Object.keys(result)) { + if (['time', 'totals'].includes(user)) continue; + + let sum = 0; + if (!result.totals.sum) result.totals.sum = 0; + + for (const type of Object.keys(result[user])) { + if (type === 'name') continue; + + if (!result.totals[type]) result.totals[type] = 0; + sum += result[user][type]; + result.totals[type] += result[user][type]; + + } + + result[user].sum = sum; + result.totals.sum += sum; + + } + + const attachment = new MessageAttachment(Buffer.from(JSON.stringify(result, null, 4)), `modstats-${guild.name}-${now}.json`); + return invoker.reply({ files: [attachment] }); + + } + +} + +module.exports = ModstatsCommand; \ No newline at end of file