community-relations/src/commands/billing.ts

56 lines
2.0 KiB
TypeScript

import axios from 'axios';
import moment from 'moment';
import { Message } from 'eris';
import { randomBytes } from 'crypto';
import { v4 as uuid } from 'uuid';
import { Client, Command } from '../class';
import Billing_T3 from './billing_t3';
export default class Billing extends Command {
constructor(client: Client) {
super(client);
this.name = 'billing';
this.description = 'Pulls up your Billing Portal. You must have a CS Account to continue.';
this.usage = `${this.client.config.prefix}billing`;
this.subcmds = [Billing_T3];
this.permissions = 0;
this.guildOnly = false;
this.enabled = true;
}
public async run(message: Message) {
try {
const response = <{
found: boolean,
emailAddress?: string,
tier?: number,
supportKey?: string,
}> (await axios.get(`https://api.cloud.libraryofcode.org/wh/info?id=${message.author.id}&authorization=${this.client.config.internalKey}`)).data;
if (!response.found) return this.error(message.channel, 'CS Account not found.');
const portalKey = randomBytes(50).toString('hex');
const uid = uuid();
const redirect = new this.client.db.Redirect({
key: uid,
to: `https://loc.sh/dash?q=${portalKey}`,
});
const portal = new this.client.db.CustomerPortal({
key: portalKey,
username: message.author.username,
userID: message.author.id,
emailAddress: response.emailAddress,
expiresOn: moment().add(5, 'minutes').toDate(),
used: false,
});
await portal.save();
await redirect.save();
const chan = await this.client.getDMChannel(message.author.id);
await chan.createMessage(`__***Billing Account Portal***__\nClick here: https://loc.sh/${uid}\n\nYou will be redirected to your billing portal, please note the link expires after 5 minutes.`);
return await this.success(message.channel, 'Your Billing Portal information has been DMed to you.');
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}