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: 'MUTE',
|
|
|
|
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,
|
2020-07-04 12:23:10 +02:00
|
|
|
color: 0x1f75ff,
|
2020-06-16 00:15:13 +02:00
|
|
|
dictionary: {
|
|
|
|
past: 'muted',
|
|
|
|
present: 'mute'
|
2020-07-16 09:54:39 +02:00
|
|
|
},
|
|
|
|
points: opts.points,
|
|
|
|
expiration: opts.expiration,
|
|
|
|
data: opts.data
|
2020-06-16 00:15:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
this.member = opts.target;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute() {
|
|
|
|
|
|
|
|
const setting = this.guild._settings.mute;
|
|
|
|
|
|
|
|
let role = null;
|
|
|
|
if(setting.type !== 2) {
|
|
|
|
role = await this.client.resolver.resolveRole(setting.role, true, this.guild);
|
|
|
|
}
|
|
|
|
|
|
|
|
let removed = [];
|
|
|
|
switch(setting.type) {
|
|
|
|
case 0:
|
|
|
|
try {
|
|
|
|
this.member.roles.add(role, this._reason);
|
|
|
|
} catch(e) {
|
2020-07-04 12:23:10 +02:00
|
|
|
return this._fail('C_MUTE_1FAIL');
|
2020-06-16 00:15:13 +02:00
|
|
|
}
|
|
|
|
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}`);
|
2020-07-04 12:23:10 +02:00
|
|
|
return this._fail('C_MUTE_2FAIL');
|
2020-06-16 00:15:13 +02:00
|
|
|
}
|
|
|
|
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}`);
|
2020-07-04 12:23:10 +02:00
|
|
|
return this._fail('C_MUTE_3FAIL');
|
2020-06-16 00:15:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.data = {
|
|
|
|
removedRoles: removed.map((r) => r.id),
|
|
|
|
muteType: setting.type,
|
2020-07-04 12:23:10 +02:00
|
|
|
muteRole: role ? role.id : null
|
2020-06-16 00:15:13 +02:00
|
|
|
}; //Info will be saved in database and into the callback when resolved.
|
|
|
|
|
2020-07-04 12:23:10 +02:00
|
|
|
const callbacks = this.client.moderationManager.callbacks.filter((c) => c.infraction.type === 'MUTE'
|
|
|
|
&& c.infraction.target === this.target.id);
|
|
|
|
|
|
|
|
if(callbacks.size > 0) callbacks.map((c) => this.client.moderationManager._removeExpiration(c));
|
|
|
|
|
|
|
|
await this.handle();
|
2020-06-16 00:15:13 +02:00
|
|
|
return this._succeed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-16 09:54:39 +02:00
|
|
|
async verify() {
|
|
|
|
|
|
|
|
if(this.guild._settings.mute.type !== 2) {
|
|
|
|
if(!this.guild._settings.mute.role) return this._fail('C_MUTE_NOMUTEROLE');
|
|
|
|
|
|
|
|
const role = await this.client.resolver.resolveRole(this.guild._settings.mute.role, true, this.guild);
|
|
|
|
if(!role) {
|
|
|
|
this.guild._updateSettings({
|
|
|
|
mute: {
|
|
|
|
...this.guild._settings.mute,
|
|
|
|
role: null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this._fail('C_MUTE_INVALIDMUTEROLE');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super._verify();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-16 00:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Mute;
|