galactic-bot/structure/client/components/commands/administration/MassBan.js
2021-06-17 17:11:55 -07:00

152 lines
6.2 KiB
JavaScript

const { Command } = require('../../../../interfaces/');
const { Ban } = require('../../../../moderation/infractions/');
class MassBanCommand extends Command {
constructor(client) {
super(client, {
name: 'massban',
module: 'administration',
usage: "<arguments..> [reason..]",
clientPermissions: ['BAN_MEMBERS'],
memberPermissions: ['MANAGE_GUILD', 'ADMINISTRATOR'],
aliases: [],
examples: [
"--accountage 1d raiding the server"
],
restricted: true,
archivable: false,
arguments: [
{
name: 'exact',
usage: '<text>',
type: 'STRING',
types: ['FLAG'],
required: true
},
{
name: 'contains',
aliases: ['includes'],
usage: '<text>',
type: 'STRING',
types: ['FLAG'],
required: true
},
{
name: 'startswith',
aliases: [
'starts',
'begins',
'beginswith'
],
usage: '<text>',
type: 'STRING',
types: ['FLAG'],
required: true
},
{
name: 'endswith',
aliases: ['ends'],
usage: '<text>',
type: 'STRING',
types: ['FLAG'],
required: true
},
{
name: 'accountage',
aliases: ['accage'],
type: 'TIME',
usage: '<time>',
types: ['FLAG'],
required: true
},
{
name: 'guildage',
aliases: ['serverage'],
type: 'TIME',
usage: '<time>',
types: ['FLAG'],
required: true
},
{
name: 'ignorecase',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'or',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'silent',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
}
],
guildOnly: true,
showUsage: true,
throttling: {
usages: 2,
duration: 60
}
});
this.client = client;
}
async execute(message, { qParams, args }) {
// const requiredArguments = ['exact', 'contains', 'startswith', 'endswith', 'accountage', 'guildage'];
// if(!Object.keys(args).some((a) => requiredArguments.includes(a))) {
// return message.respond(message.format('C_MASSBAN_REQUIREDARGUMENTS'), {
// emoji: 'failure'
// });
// }
const members = await message.guild.members.fetch();
const filteredMembers = this._filterMembers(members, args);
if(filteredMembers.size === 0) {
return message.respond("Found nothing");
}
return message.respond(filteredMembers.map((m) => `${m.user.tag} (${m.user.id})`).join('\n'));
}
_filterMembers(members, a) {
const filters = {
AND: (m) => {
return (a.exact ? a.ignorecase ? a.exact.value.toLowerCase() === m.user.username.toLowerCase() : a.exact.value === m.user.username : true) //eslint-disable-line no-nested-ternary
&& (a.contains ? a.ignorecase ? m.user.username.toLowerCase().includes(a.contains.value.toLowerCase()) : m.user.username.includes(a.contains.value) : true) //eslint-disable-line no-nested-ternary
&& (a.startswith ? a.ignorecase ? m.user.username.toLowerCase().startsWith(a.startswith.value.toLowerCase()) : m.user.username.startsWith(a.startswith.value) : true) //eslint-disable-line no-nested-ternary
&& (a.endswith ? a.ignorecase ? m.user.username.toLowerCase().endsWith(a.endswith.value.toLowerCase()) : m.user.username.endsWith(a.endswith.value) : true) //eslint-disable-line no-nested-ternary
&& (a.accountage ? Math.floor((Date.now() - m.user.createdTimestamp)/1000) < a.accountage.value : true)
&& (a.guildage ? Math.floor((Date.now() - m.joinedTimestamp)/1000) < a.guildage.value : true);
},
OR: (m) => {
return (a.exact ? a.ignorecase ? a.exact.value.toLowerCase() === m.user.username.toLowerCase() : a.exact.value === m.user.username : false) //eslint-disable-line no-nested-ternary
|| (a.contains ? a.ignorecase ? m.user.username.toLowerCase().includes(a.contains.value.toLowerCase()) : m.user.username.includes(a.contains.value) : false) //eslint-disable-line no-nested-ternary
|| (a.startswith ? a.ignorecase ? m.user.username.toLowerCase().startsWith(a.startsWith.value.toLowerCase()) : m.user.username.startsWith(a.startswith.value) : false) //eslint-disable-line no-nested-ternary
|| (a.endswith ? a.ignorecase ? m.user.username.toLowerCase().endsWith(a.endswith.value.toLowerCase()) : m.user.username.endsWith(a.endswith.value) : false) //eslint-disable-line no-nested-ternary
|| (a.accountage ? Math.floor((Date.now() - m.user.createdTimestamp)/1000) < a.accountage.value : false)
|| (a.guildage ? Math.floor((Date.now() - m.joinedTimestamp)/1000) < a.guildage.value : false);
}
};
const method = a.or ? 'OR' : 'AND';
return members.filter((m) => {
return filters[method](m);
});
}
}
module.exports = MassBanCommand;