galactic-bot/structure/moderation/infractions/Unmute.js
2020-06-15 17:15:13 -05:00

103 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: 0xf7b045,
dictionary: {
past: 'unmuted',
present: 'unmute'
},
data: opts.data
});
this.client = client;
this.member = opts.target;
}
async execute() {
const setting = this.guild._settings.mute;
let removedRoles = [],
muteType = null,
role = 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.role; //eslint-disable-line prefer-destructuring
} else {
const mute = await this.member._checkMute();
if(!mute) return this._fail(this.message.format('C_UNMUTE_CANNOTFINDMUTE'));
removedRoles = mute.data.removedRoles; //eslint-disable-line prefer-destructuring
muteType = mute.data.muteType; //eslint-disable-line prefer-destructuring
role = mute.data.role; //eslint-disable-line prefer-destructuring
}
role = await this.client.resolver.resolveRole(setting.role, true, this.guild);
if(!role) {
}
let removed = [];
switch(setting.type) {
case 0:
try {
this.member.roles.add(role, this._reason);
} catch(e) {
return this._fail(this.message.format('C_MUTE_1FAIL'));
}
break;
case 1:
removed = this.member.roles.cache.filter((r) => !r.managed && r.comparePositionTo(this.guild.me.roles.highest) < 0 && r.id !== this.guild.id);
try {
this.member.roles.set([
...this.member.roles.cache.filter((r) => r.managed || r.comparePositionTo(this.guild.me.roles.highest) > 0 || r.id === this.guild.id).array(),
role
], this._reason);
} catch(error) {
this.client.logger.error(`Mute infraction failed to calculate removeable roles, might want to check this out.\n${error.stack || error}`);
return this._fail(this.message.format('C_MUTE_2FAIL'));
}
break;
case 2:
removed = this.member.roles.cache.filter((r) => !r.managed && r.comparePositionTo(this.guild.me.roles.highest) < 0 && r.id !== this.guild.id);
try {
this.member.roles.set(this.member.roles.cache.filter((r) => r.managed || r.comparePositionTo(this.guild.me.roles.highest) > 0 || r.id === this.guild.id), this._reason);
} catch(error) {
this.client.logger.error(`Mute infraction failed to calculate removeable roles, might want to check this out.\n${error.stack || error}`);
return this._fail(this.message.format('C_MUTE_3FAIL'));
}
break;
}
this.data = {
removedRoles: removed.map((r) => r.id),
muteType: setting.type
}; //Info will be saved in database and into the callback when resolved.
await this.log();
return this._succeed();
}
}
module.exports = Mute;