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

107 lines
3.7 KiB
JavaScript

/* eslint-disable indent */
const { Infraction } = require('../interfaces/');
class UnmuteInfraction 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,
guild: opts.guild,
channel: opts.channel,
arguments: opts.arguments,
silent: opts.silent,
duration: opts.duration,
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
} else {
role = this.guild._settings.mute.role; //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();
}
}
module.exports = UnmuteInfraction;