galactic-bot/structure/moderation/infractions/Unmute.js
2020-07-28 13:40:15 -07:00

121 lines
3.9 KiB
JavaScript

/* eslint-disable indent */
const { Infraction } = require('../interfaces/');
class Mute extends Infraction {
constructor(client, opts = {}) {
super(client, {
type: 'UNMUTE',
targetType: 'user',
message: opts.message,
executor: opts.executor.user,
target: opts.target.user,
reason: opts.reason || 'N/A',
guild: opts.guild,
channel: opts.channel,
arguments: opts.arguments,
silent: opts.silent,
duration: opts.duration,
color: 0x5c9aff,
dictionary: {
past: 'unmuted',
present: 'unmute'
},
hyperlink: opts.hyperlink,
points: opts.points,
expiration: opts.expiration,
data: opts.data
});
this.client = client;
this.member = opts.target;
}
async execute() {
let removedRoles = [],
muteType = null,
role = null;
let mute = null;
if(Object.keys(this.data).length) {
removedRoles = this.data.removedRoles; //eslint-disable-line prefer-destructuring
muteType = this.data.muteType; //eslint-disable-line prefer-destructuring
role = this.data.muteRole; //eslint-disable-line prefer-destructuring
} else {
mute = await this.member._getExpiration('MUTE');
if(mute) {
removedRoles = mute.infraction.data.removedRoles; //eslint-disable-line prefer-destructuring
muteType = mute.infraction.data.muteType; //eslint-disable-line prefer-destructuring
role = mute.infraction.data.muteRole; //eslint-disable-line prefer-destructuring
}
}
role = await this.client.resolver.resolveRole(role, true, this.guild);
if(!mute) {
if(role && this.member.roles.cache.has(role.id)) {
try {
this.member.roles.remove(role, this._reason);
} catch(e) {
return this._fail('C_UNMUTE_1FAIL');
}
} else {
return this._fail('C_UNMUTE_CANNOTFINDMUTE');
}
}
const roles = [...new Set([ ...this.member.roles.cache.map((r) => r.id), ...removedRoles])];
switch(muteType) {
case 0:
if(!role) this._fail('C_UNMUTE_ROLEDOESNTEXIST');
try {
this.member.roles.remove(role, this._reason);
} catch(e) {
return this._fail('C_UNMUTE_1FAIL');
}
break;
case 1:
if(role) {
const index = roles.indexOf(role.id);
roles.splice(index, 1);
}
try {
this.member.roles.set(roles, this._reason);
} catch(error) {
this.client.logger.error(`Unmute infraction failed to calculate additional roles, might want to check this out.\n${error.stack || error}`);
return this._fail('C_UNMUTE_2FAIL');
}
break;
case 2:
try {
this.member.roles.set(roles, this._reason);
} catch(error) {
this.client.logger.error(`Unmute infraction failed to calculate additional roles, might want to check this out.\n${error.stack || error}`);
return this._fail('C_UNMUTE_3FAIL');
}
break;
}
await this.handle();
return this._succeed();
}
async verify() {
const missing = this.guild._checkPermissions(this.message, 'command:unmute');
if(missing.length > 0) {
return super._fail('C_UNMUTE_INSUFFICIENTPERMISSIONS');
}
return super._verify();
}
}
module.exports = Mute;