galactic-bot/structure/moderation/infractions/Nickname.js
2020-07-19 15:42:21 -07:00

90 lines
4.1 KiB
JavaScript

const { Infraction } = require('../interfaces/');
const escapeRegex = require('escape-string-regexp');
const Constants = {
Characters: ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '=', '+', '"', "'", '`', '\\', '/', '[', ']', '>', '<', '.'],
RandomNames: ["Ashly Armistead", "Jenifer James", "Eleonora Espinosa", "Eleanore Ellerbee", "Marguerite Means", "Lore Lazar", "Cherilyn Calero", "Olevia Olvera", "Herman Horvat", "Elvin Elfrink", "Jennine Julian", "Pei Plude", "Kenisha Kirts", "Sherly Starbird", "Lashanda Louder", "Elizabeth Eakle", "Lyda Leard", "Penelope Petri", "Kimi Kissel", "Crista Crowe", "Mallory Mehta", "Bettina Bregman", "Marcie Moran", "James Jiles", "Clifton Capshaw", "Cher Curlee", "Brigid Bonier", "Lana Lehman", "Carletta Chalker", "Valene Viator", "Eugena Eder", "Samatha Shivers", "Hoyt Hard", "Karena King", "Tiffani Trott", "Joye Jeremiah", "Emely Espino", "Rowena Rowan", "Gillian Godbey", "Hailey Huggard", "Lillian Lander", "Shani Szabo", "Liberty Lamorte", "Tomika Thrift", "Garrett Grogan", "Vallie Valazquez", "Shin Secor", "Deangelo Divito", "Kacy Kimball", "Norris Northway", "Evelia Ernest", "Horacio Huey", "Christen Cyr", "Jada Janus", "Vanita Valero", "Abdul Addario", "Daniell Dingle", "Tangela Trull", "Jeanmarie Jimenez", "Georgia Gourlay", "Valery Vergara", "Chasidy Conrad", "Marlena Malin", "Kristeen Kesselman", "Jo Jeffries", "Terina Tworek", "Geri Gilbert", "Phuong Pratte", "Juanita Jess", "Audrea Ammon", "Shakita Stalter", "Rosamond Reese", "Shani Solorio", "Archie Augustyn", "Bridgett Behrman", "Susann Schmaltz", "Edgardo Engman", "Valerie Vice", "Cheyenne Claeys", "Kristina Kastner", "Lanita Labrum", "Kai Knick", "Lucien Lee", "Tameika Turley", "Octavia Obermiller", "Kia Kravitz", "Justina Jury", "Cortney Coppa", "Johnathan Joshi", "Arden Alphin", "Francis Feinberg", "Sal Suits", "Myles Marden", "Emmitt Engelmann", "Tara Tibbits", "Su Sprowl", "Isadora Ingrassia", "Janell Jarrard", "Kenyatta Kenley", "Mabelle Mccrady"]
};
const Regex = new RegExp(`(^(?:${Constants.Characters.map((c) => escapeRegex(c)).join('|')}*)*)(.*)`, 'gui');
class Nickname extends Infraction {
constructor(client, opts = {}) {
super(client, {
type: 'NICKNAME',
targetType: 'user',
executor: opts.executor.user,
target: opts.target.user,
reason: opts.reason || 'N/A',
guild: opts.guild,
channel: opts.channel,
color: 0xEBEBEB,
silent: opts.silent,
dictionary: {
past: 'nicknamed',
present: 'nickname'
},
points: opts.points,
expiration: opts.expiration,
data: opts.data
});
this.client = client;
this.member = opts.target;
}
async execute() {
let { dehoist, name } = this.data; //eslint-disable-line prefer-const
if(dehoist) {
const displayName = this.member.nickname || this.member.user.username;
const [, removed, fixed] = Regex.exec(displayName);
if(!removed) return this._fail('C_NICKNAME_NOCHARACTERS');
if(fixed) {
name = fixed;
} else {
name = this._generateName();
}
}
try {
this.member.setNickname(name, this._reason);
} catch(e) {
return this._fail('INFRACTION_ERROR');
}
await this.handle();
return this._succeed();
}
_generateName() {
const random = Math.floor(Math.random()*Constants.RandomNames.length);
return Constants.RandomNames[random];
}
verify() {
const missing = this.channel.permissionsFor(this.guild.me).missing(['MANAGE_NICKNAMES']);
if(missing.length > 0) {
return super._fail('C_NICKNAME_INSUFFICIENTPERMISSIONS');
}
const { highest } = this.member.roles;
if(highest.comparePositionTo(this.guild.me.roles.highest) > 0) {
return this._fail('C_NICKNAME_MISSINGPERMISSIONS');
}
return super._verify();
}
}
module.exports = Nickname;