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

121 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-06-16 00:15:13 +02:00
/* 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,
2020-06-16 00:15:13 +02:00
dictionary: {
past: 'unmuted',
present: 'unmute'
},
hyperlink: opts.hyperlink,
2020-07-16 09:54:39 +02:00
points: opts.points,
expiration: opts.expiration,
2020-06-16 00:15:13 +02:00
data: opts.data
});
this.client = client;
this.member = opts.target;
}
async execute() {
let removedRoles = [],
muteType = null,
role = null;
let mute = null;
2020-06-16 00:15:13 +02:00
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
2020-06-16 00:15:13 +02:00
} else {
mute = await this.member._getExpiration('MUTE');
if(mute) {
2020-07-20 00:42:21 +02:00
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
}
2020-06-16 00:15:13 +02:00
}
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');
}
2020-06-16 00:15:13 +02:00
}
const roles = [...new Set([ ...this.member.roles.cache.map((r) => r.id), ...removedRoles])];
switch(muteType) {
2020-06-16 00:15:13 +02:00
case 0:
if(!role) this._fail('C_UNMUTE_ROLEDOESNTEXIST');
2020-06-16 00:15:13 +02:00
try {
this.member.roles.remove(role, this._reason);
2020-06-16 00:15:13 +02:00
} catch(e) {
return this._fail('C_UNMUTE_1FAIL');
2020-06-16 00:15:13 +02:00
}
break;
case 1:
if(role) {
const index = roles.indexOf(role.id);
roles.splice(index, 1);
}
2020-06-16 00:15:13 +02:00
try {
this.member.roles.set(roles, this._reason);
2020-06-16 00:15:13 +02:00
} 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');
2020-06-16 00:15:13 +02:00
}
break;
case 2:
try {
this.member.roles.set(roles, this._reason);
2020-06-16 00:15:13 +02:00
} 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');
2020-06-16 00:15:13 +02:00
}
break;
}
await this.handle();
2020-06-16 00:15:13 +02:00
return this._succeed();
}
2020-07-20 00:42:21 +02:00
async verify() {
2020-07-28 22:40:15 +02:00
const missing = this.guild._checkPermissions(this.message, 'command:unmute');
2020-07-20 00:42:21 +02:00
if(missing.length > 0) {
return super._fail('C_UNMUTE_INSUFFICIENTPERMISSIONS');
}
return super._verify();
}
2020-06-16 00:15:13 +02:00
}
module.exports = Mute;