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

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-07-16 10:19:31 +02:00
/* eslint-disable indent */
const { Infraction } = require('../interfaces/');
class AddroleInfraction extends Infraction {
static type = 'ADDROLE';
2020-07-16 10:19:31 +02:00
constructor(client, opts = {}) {
super(client, {
targetType: 'USER',
type: opts.type,
2020-07-16 10:19:31 +02:00
guild: opts.guild,
channel: opts.channel,
message: opts.message,
target: opts.target.user,
executor: opts.executor.user,
reason: opts.reason,
2020-07-16 10:19:31 +02:00
arguments: opts.arguments,
silent: opts.silent,
duration: opts.duration,
points: opts.points,
expiration: opts.expiration,
data: opts.data,
hyperlink: opts.hyperlink
2020-07-16 10:19:31 +02:00
});
this.client = client;
2020-07-20 00:42:21 +02:00
this.member = opts.target;
this.executorMember = opts.executor;
2020-07-16 10:19:31 +02:00
}
async execute() {
try {
await this.member.roles.add(this.data.roleIds, this._reason);
2020-07-16 10:19:31 +02:00
} catch(error) {
return this._fail('INFRACTION_ERROR');
}
await this.handle();
return this._succeed();
}
async verify() {
2020-07-28 22:40:15 +02:00
const missingAdministrator = this.channel.permissionsFor(this.guild.me).missing(['ADMINISTRATOR']);
let filtered = [];
const { highest } = this.guild.me.roles;
filtered = this.data.roles.filter((r) => r.comparePositionTo(highest) < 0);
if(filtered.length === 0) {
return super._fail('C_ADDROLE_ROLEHIERARCHYBOT');
}
2020-07-28 22:40:15 +02:00
if(missingAdministrator.length > 0) {
const { highest } = this.executorMember.roles;
filtered = this.data.roles.filter((r) => r.comparePositionTo(highest) < 0);
if(filtered.length === 0) {
return super._fail('C_ADDROLE_ROLEHIERARCHY');
}
2020-07-16 10:19:31 +02:00
}
this.data.roles = filtered;
return super._verify();
}
description(dm) {
return `\n${this.guild.format('INFRACTION_DESCRIPTIONROLES', {
plural: this.data.roleIds.length === 1 ? '' : 's',
roles: dm ? this.data.roleNames.join(', ') : this.data.roleIds.map((r) => `<@&${r}>`).join(' ')
})}`;
}
2020-07-16 10:19:31 +02:00
}
module.exports = AddroleInfraction;