galactic-bot/structure/client/components/commands/moderation/History.js
2020-08-09 12:53:09 -07:00

129 lines
4.4 KiB
JavaScript

const { Command } = require('../../../../interfaces/');
const { stripIndents } = require('common-tags');
const { Util } = require('../../../../../util/');
const pageSize = 10;
class HistoryCommand extends Command {
constructor(client) {
super(client, {
name: 'history',
module: 'moderation',
usage: "[user..|channel..]",
aliases: [
'moderation'
],
memberPermissions: ['MANAGE_MESSAGES'],
guildOnly: true,
arguments: [
{
name: 'before', //Search for moderation actions before x
usage: '<date>',
type: 'DATE',
types: ['FLAG'],
required: true
},
{
name: 'after', //Search for moderation actions after x
usage: '<date>',
type: 'DATE',
types: ['FLAG'],
required: true
},
{
name: 'export', //Export moderation actions in a JSON.
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'private', //Send moderation history in DMs.
type: 'BOOLEAN',
types: ['FLAG'],
default: true
} //filter, exclude, verbose (NO PAGESIZE)
],
throttling: {
usages: 2,
duration: 5
}
});
this.client = client;
}
async execute(message, { params, args }) {
const query = {
guild: message.guild.id
};
const { parsed, parameters } = await this.client.resolver.infinite(params, [
this.client.resolver.resolveMember.bind(this.client.resolver),
this.client.resolver.resolveUser.bind(this.client.resolver),
this.client.resolver.resolveChannel.bind(this.client.resolver)
], true, message.guild, (c) => c.type === 'text');
if(parsed.length > 0) query.target = { $in: parsed.map((p) => p.id) }; //Add resolved ids to the query.
if(args.before || args.after) {
query.timestamp = {};
if(args.before) query.timestamp.$lt = args.before.value.valueOf(); //Add before timestamps to the query.
if(args.after) query.timestamp.$gt = args.after.value.valueOf(); //Add after timestamps to the query.
}
let page = 1;
if(parameters.length > 0) {
const number = parseInt(parameters[0]);
if(!Number.isNaN(number) && number > 1) {
page = number;
}
}
const collectionSize = await this.client.storageManager.mongodb.infractions.count(query);
if(collectionSize === 0) {
return message.respond(message.format('C_HISTORY_NORESULTS'), {
emoji: 'failure'
});
}
const maxPage = Math.ceil(collectionSize/pageSize);
if(page > maxPage) page = maxPage;
const infractions = await this.client.storageManager.mongodb.db.collection('infractions').find(query)
.sort({ timestamp: -1 }).skip((page-1)*pageSize).limit(pageSize).toArray();
const embed = {
author: {
name: 'Search Results',
icon_url: message.guild.iconURL() //eslint-disable-line camelcase
},
fields: [],
footer: {
text: `Page ${page}/${maxPage} | ${collectionSize} Results`
}
};
for(const infraction of infractions) {
let target = null;
if(infraction.targetType === 'user') {
target = await this.client.resolver.resolveUser(infraction.target);
} else {
target = await this.client.resolver.resolveChannel(infraction.target, true, message.guild);
}
embed.fields.push({
name: `**[${infraction.case}] ${infraction.type}** ${target ? `*${infraction.targetType === 'user' ? Util.escapeMarkdown(target.tag) : `#${target.name}`}*` : `Missing ${infraction.targetType}`}`,
value: stripIndents`**Reason: ** \`${infraction.reason}\``
});
}
message.embed(embed);
}
}
module.exports = HistoryCommand;