community-relations/src/commands/judgement_add.ts

92 lines
3.5 KiB
TypeScript

import moment, { unitOfTime } from 'moment';
import { nanoid } from 'nanoid';
import { Message, TextChannel } from 'eris';
import { Client, Command, RichEmbed } from '../class';
export default class Judgement_Add extends Command {
constructor(client: Client) {
super(client);
this.name = 'add';
this.description = 'Creates a judgement.';
this.usage = `${this.client.config.prefix}judgement add <member> <severity: 0 LOW, 1 MEDIUM, 2 HIGH> <time: 0 for indefinite> <description>`;
this.permissions = 5;
this.guildOnly = true;
this.enabled = true;
}
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, ['judgement', 'add']);
if (args.length < 4) return this.client.commands.get('help').run(message, ['judgement', 'add']);
const member = this.client.util.resolveMember(args[0], this.mainGuild);
if (!member) return this.error(message.channel, 'Unable to locate member.');
const staff = await this.client.db.mongo.Staff.findOne({ userID: message.author.id }).lean().exec();
if (!staff) return this.error(message.channel, 'Unable to locate Staff information.');
if (Number.isNaN(Number(args[1]))) return this.error(message.channel, 'Severity must be a number.');
if (Number(args[1]) < 0 || Number(args[1]) > 2) return this.error(message.channel, 'Severity must be greater than -1 and less than 3.');
let momentMilliseconds: number;
const now: number = Date.now();
let date: Date;
if (args[2] !== '0') {
const lockLength = args[2].match(/[a-z]+|[^a-z]+/gi);
const length = Number(lockLength[0]);
const unit = lockLength[1] as unitOfTime.Base;
momentMilliseconds = moment.duration(length, unit).asMilliseconds();
date = new Date(momentMilliseconds + now);
}
const jid = nanoid(11);
const entry = new this.client.db.mongo.Judgement({
jid,
userID: member.user.id,
enteredBy: message.author.id,
severity: Number(args[1]),
date: new Date(),
expires: date ?? undefined,
description: args.slice(3).join(' '),
});
await entry.save();
let severity: string;
switch (Number(args[1])) {
case 0:
severity = 'LOW';
break;
case 1:
severity = 'MEDIUM';
break;
case 2:
severity = 'HIGH';
break;
default:
break;
}
const log = <TextChannel> this.client.guilds.get(this.client.config.guildID).channels.get('611584771356622849');
const embed = new RichEmbed();
embed.setTitle('Judgement - Creation');
embed.setDescription(entry.description);
embed.addField('Member', `${member.username}#${member.discriminator} (<@${member.id}>)`, true);
embed.addField('Entered by', `${message.author.username}, ${staff.pn.slice(0, 2).join(', ')}`, true);
embed.addField('Severity', severity, true);
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
if (date) {
embed.addField('Expires on', date.toLocaleString('en-us'), true);
} else {
embed.addField('Expires on', 'INDEFINITE', true);
}
log.createMessage({ embed });
return this.success(message.channel, `Judgement \`${jid}\` successfully recorded.`);
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}