galactic-bot/structure/moderation/infractions/Ban.js

78 lines
2.1 KiB
JavaScript

/* eslint-disable indent */
const { Infraction } = require('../interfaces/');
const { GuildMember } = require('../../extensions/');
class BanInfraction extends Infraction {
constructor(client, opts = {}) {
super(client, {
type: 'BAN',
targetType: 'USER',
guild: opts.guild,
channel: opts.channel,
message: opts.message,
executor: opts.executor.user,
target: opts.target instanceof GuildMember ? opts.target.user : opts.target,
reason: opts.reason,
arguments: opts.arguments,
silent: opts.silent,
duration: opts.duration,
points: opts.points,
expiration: opts.expiration,
data: opts.data
});
this.client = client;
}
async execute() {
let days = 0;
if(this.message._caller === 'hardban') {
days = 1;
if(this.arguments.days) {
days = this.arguments.days.value;
}
}
try {
await this.guild.members.ban(this.target.id, {
reason: this._reason,
days
});
} catch(error) {
return this._fail('INFRACTION_ERROR');
}
const callbacks = this.client.moderationManager.callbacks.filter((c) => c.infraction.type === 'BAN'
&& c.infraction.target === this.target.id);
if(callbacks.size > 0) callbacks.map((c) => this.client.moderationManager._removeExpiration(c));
await this.handle();
return this._succeed();
}
async verify() {
if(this.member instanceof GuildMember) {
if(!this.member.bannable) return super._fail('C_BAN_CANNOTBEBANNED');
}
let alreadyBanned = null;
try {
alreadyBanned = await this.guild.fetchBan(this.member.id);
} catch(e) {} //eslint-disable-line no-empty
if(alreadyBanned) return super._fail('C_BAN_ALREADYBANNED');
return super._verify();
}
}
module.exports = BanInfraction;