galactic-bot/structure/moderation/infractions/Addrole.js
2021-06-26 14:08:04 -07:00

82 lines
2.5 KiB
JavaScript

/* eslint-disable indent */
const { Infraction } = require('../interfaces/');
class AddroleInfraction extends Infraction {
static type = 'ADDROLE';
constructor(client, opts = {}) {
super(client, {
targetType: 'USER',
type: opts.type,
guild: opts.guild,
channel: opts.channel,
message: opts.message,
target: opts.target.user,
executor: opts.executor.user,
reason: opts.reason,
arguments: opts.arguments,
silent: opts.silent,
duration: opts.duration,
points: opts.points,
expiration: opts.expiration,
data: opts.data,
hyperlink: opts.hyperlink
});
this.client = client;
this.member = opts.target;
this.executorMember = opts.executor;
}
async execute() {
try {
await this.member.roles.add(this.data.roleIds, this._reason);
} catch(error) {
return this._fail('INFRACTION_ERROR');
}
await this.handle();
return this._succeed();
}
async verify() {
const missingAdministrator = this.channel.permissionsFor(this.guild.me).missing(['ADMINISTRATOR']);
let filtered = [];
const { highest: clientHighest } = this.guild.me.roles;
filtered = this.data.roles.filter((r) => r.comparePositionTo(clientHighest) < 0);
console.log("Filtered roles (client)...", filtered.map((r) => r.name).join(', '));
if(filtered.length === 0) {
return super._fail('C_ADDROLE_ROLEHIERARCHYBOT');
}
if(missingAdministrator.length > 0) {
const { highest: memberHighest } = this.executorMember.roles;
console.log(memberHighest.map((r) => r.name));
filtered = this.data.roles.filter((r) => r.comparePositionTo(memberHighest) < 0);
console.log("Filtered roles (member)...", filtered.map((r) => r.name).join(', '));
if(filtered.length === 0) {
return super._fail('C_ADDROLE_ROLEHIERARCHY');
}
}
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(' ')
})}`;
}
}
module.exports = AddroleInfraction;