community-relations/src/commands/offer.ts

51 lines
2.4 KiB
TypeScript

/* eslint-disable default-case */
import jwt from 'jsonwebtoken';
import { Message } from 'eris';
import { Client, Command } from '../class';
export default class Offer extends Command {
constructor(client: Client) {
super(client);
this.name = 'offer';
this.description = 'Pre-qualifies a member for an offer. Will run a hard-pull automatically on acceptance.';
this.usage = `${this.client.config.prefix}offer <member> <name of offer>:<department/service for hard pull>`;
this.permissions = 4;
this.aliases = ['qualify'];
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, [this.name]);
const member = this.client.util.resolveMember(args[0], this.mainGuild);
if (!member) return this.error(message.channel, 'Could not find member.');
const score = await this.client.db.Score.findOne({ userID: member.user.id }).lean().exec();
if (!score) return this.error(message.channel, 'Could not find score report for this user.');
if (score.locked) return this.error(message.channel, 'This user\'s score report is locked.');
const name = args.slice(1).join(' ').split(':')[0];
const dept = args.slice(1).join(' ').split(':')[1];
if (!name || !dept) return this.error(message.channel, 'Invalid arguments.');
const token = jwt.sign({
userID: member.user.id,
staffID: message.author.id,
channelID: message.channel.id,
messageID: message.id,
pin: score.pin.join('-'),
name,
department: dept,
date: new Date(),
}, this.client.config.internalKey, { expiresIn: '12h' });
this.client.getDMChannel(member.user.id).then((chan) => {
chan.createMessage(`__**Offer Pre-Qualified**__\nYou have been pre-approved for an offer! If you wish to accept this offer, please enter the offer code at https://report.libraryofcode.org/. Do not share this code with anyone else. This offer automatically expires in 12 hours. Your report will be Hard Inquiried immediately after accepting this offer.\n\n**Department:** ${dept.toUpperCase()}\n**Offer for:** ${name}\n\n\`${token}\``);
}).catch(() => this.error(message.channel, 'Could not DM member.'));
return this.success(message.channel, `Offer sent.\n\n\`${token}\``);
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}