galactic-bot/structure/moderation/infractions/Addrole.js
2020-07-28 13:40:15 -07:00

75 lines
2.0 KiB
JavaScript

/* eslint-disable indent */
const { Infraction } = require('../interfaces/');
class AddroleInfraction extends Infraction {
constructor(client, opts = {}) {
super(client, {
type: 'ADDROLE',
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: 0xff3333,
dictionary: {
past: 'added role to',
present: 'add role to'
},
points: opts.points,
expiration: opts.expiration,
data: opts.data
});
this.client = client;
this.member = opts.target;
this.executorMember = opts.executor;
}
async execute() {
try {
await this.member.roles.add(this.data.roles, this._reason);
} catch(error) {
return this._fail('INFRACTION_ERROR');
}
await this.handle();
return this._succeed();
}
async verify() {
const missing = this.guild._checkPermissions(this.message, 'command:addrole');
if(missing.length > 0) {
return super._fail('C_ADDROLE_INSUFFICIENTPERMISSIONS');
}
const missingAdministrator = this.channel.permissionsFor(this.guild.me).missing(['ADMINISTRATOR']);
let filtered = [];
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');
}
}
this.data.roles = filtered;
return super._verify();
}
}
module.exports = AddroleInfraction;