189 lines
7.7 KiB
JavaScript
189 lines
7.7 KiB
JavaScript
const { Command } = require('../../../../interfaces/');
|
|
const { Ban } = require('../../../../moderation/infractions/');
|
|
|
|
const { MessageAttachment } = require('discord.js');
|
|
|
|
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(message.format('C_MASSBAN_NORESULTS'), { emoji: 'failure' });
|
|
|
|
const text = filteredMembers.map((m) => `${m.user.tag} (${m.user.id})`).join('\n');
|
|
|
|
const prompt = await message.prompt(message.format('C_MASSBAN_VERIFICATION', { amount: filteredMembers.size }), {
|
|
emoji: 'warning',
|
|
files: [
|
|
new MessageAttachment(Buffer.from(text, 'utf-8'), 'members.txt')
|
|
]
|
|
});
|
|
|
|
if(!prompt) return undefined;
|
|
const boolean = this.client.resolver.resolveBoolean(prompt.content.toLowerCase());
|
|
if(boolean === null) return message.respond(message.format('C_MASSBAN_INVALIDABORT'), { emojis: 'failure' });
|
|
if(boolean === false) return message.respond(message.format('C_MASSBAN_ABORT'), { emojis: 'success' });
|
|
|
|
for(const member of filteredMembers.values()) {
|
|
const fetch = await message.guild.members.fetch(member.id);
|
|
if(!fetch) continue;
|
|
|
|
try {
|
|
await this.client.moderationManager._handleTarget(Ban, fetch, {
|
|
guild: message.guild,
|
|
channel: message.channel,
|
|
executor: message.member,
|
|
reason: qParams.join(' ')
|
|
});
|
|
} catch(e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
return message.respond(message.format('C_MASSBAN_SUCCESS'), { emojis: 'success' });
|
|
|
|
}
|
|
|
|
_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);
|
|
});
|
|
|
|
}
|
|
|
|
_alertExecutor(message, successes, fails) {
|
|
return message.respond(message.format('C_MASSBAN_COMPLETED', { success: successes, max: successes+fails }), {
|
|
reply: true,
|
|
emoji: 'success'
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = MassBanCommand; |