cloudservices/src/commands/authreferral.ts

47 lines
2.5 KiB
TypeScript

import jwt from 'jsonwebtoken';
import { Message } from 'discord.js';
import { Client, Command } from '../class';
export default class AuthReferral extends Command {
constructor(client: Client) {
super(client);
this.name = 'authreferral';
this.description = 'Requests authorization for a referral.';
this.permissions = { roles: ['662163685439045632', '701454780828221450'] };
this.enabled = true;
this.aliases = ['auth'];
this.usage = `${this.client.config.prefix}authreferral <referral code | referring user> <referred member id>`;
}
public async run(message: Message, args: string[]) { // eslint-disable-line
try {
if (!args.length) return this.client.commands.get('help').run(message, [this.name]);
const referrer = await this.client.db.Account.findOne({ $or: [{ username: args[0] }, { referralCode: args[0] }, { userID: args[0].replace(/[<@!>]/gi, '') }] });
if (!referrer) return this.error(message.channel, 'Cannot find referrer.');
const referred = await message.guild.members.fetch(args[1]);
if (!referred) return this.error(message.channel, 'Cannot find referred member.');
const token = jwt.sign(
{ staffUserID: message.author.id,
referralCode: referrer.referralCode,
referrerUserID: referrer.userID,
referrerUsername: referrer.username,
referredUserID: referred.id,
referredUserAndDiscrim: `${referred.user.username}#${referred.user.discriminator}` },
this.client.config.keyPair.privateKey, { expiresIn: '24 hours', issuer: 'Library of Code sp-us | Cloud Services Daemon' },
);
this.client.users.fetch(referrer.userID).then(async (user) => {
await user.send('__**Referral Request Authorization**__\n'
+ 'Your referral code has been used in an application recently submitted to us. We need to authorize this request, please visit https://loc.sh/rv and enter the authorization token below. This token expires in 24 hours. If you did not authorize this request, please contact us immediately by DMing Ramirez or opening a ticket at https://loc.sh/cs-help.\n'
+ `**Referred User:** ${referred.user.username}#${referred.user.discriminator} | ${referred.user.toString()}`);
await user.send(`\`${token}\``);
}).catch(() => {
this.error(message.channel, 'Could not DM referrer.');
});
return this.success(message.channel, `Sent authorization token to ${referrer.username}\n\`${token}\``);
} catch (error) {
return this.client.util.handleError(error, message, this);
}
}
}