modstats command

This commit is contained in:
Erik 2022-07-15 11:50:32 +03:00
parent 47f50758f6
commit 23d63a86e8
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

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