galactic-bot/structure/client/components/commands/administration/MassBan.js

189 lines
7.7 KiB
JavaScript
Raw Normal View History

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',
2021-06-18 02:11:55 +02:00
usage: "<arguments..> [reason..]",
clientPermissions: ['BAN_MEMBERS'],
2021-06-18 02:11:55 +02:00
memberPermissions: ['MANAGE_GUILD', 'ADMINISTRATOR'],
aliases: [],
examples: [
2021-06-18 02:11:55 +02:00
"--accountage 1d raiding the server"
],
restricted: true,
2021-06-11 03:08:31 +02:00
archivable: false,
arguments: [
{
2021-06-18 02:11:55 +02:00
name: 'exact',
usage: '<text>',
type: 'STRING',
types: ['FLAG'],
required: true
},
{
name: 'contains',
aliases: ['includes'],
usage: '<text>',
type: 'STRING',
types: ['FLAG'],
required: true
},
{
2021-06-18 02:11:55 +02:00
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',
2021-06-18 02:11:55 +02:00
usage: '<time>',
types: ['FLAG'],
2021-06-18 02:11:55 +02:00
required: true
},
{
name: 'guildage',
aliases: ['serverage'],
type: 'TIME',
usage: '<time>',
2021-06-18 02:11:55 +02:00
types: ['FLAG'],
required: true
},
{
2021-06-18 02:11:55 +02:00
name: 'ignorecase',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
2021-06-18 02:11:55 +02:00
name: 'or',
type: 'BOOLEAN',
types: ['FLAG'],
2021-06-18 02:11:55 +02:00
default: true
},
{
name: 'silent',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
}
],
guildOnly: true,
showUsage: true,
throttling: {
usages: 2,
2021-06-18 02:11:55 +02:00
duration: 60
}
});
this.client = client;
}
2021-06-18 02:11:55 +02:00
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')
]
});
2021-06-18 02:11:55 +02:00
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' });
2021-06-18 02:11:55 +02:00
}
_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;