diff --git a/src/class/Report.ts b/src/class/Report.ts index 711d606..38755fe 100644 --- a/src/class/Report.ts +++ b/src/class/Report.ts @@ -1,5 +1,6 @@ import type { AxiosError } from 'axios'; import axios from 'axios'; +import { response } from 'express'; export interface SoftReport { status: 'SUCCESS' | 'UNAUTHORIZED' | 'PERMISSION_DENIED' | 'CLIENT_ERROR' | 'SERVER_ERROR'; @@ -18,6 +19,24 @@ export interface HardReport extends SoftReport { } export default class Report { + public static async tier2(userID: string, auth: string) { + try { + const { data } = await axios({ + method: 'get', + url: `https://eds.libraryofcode.org/cs/t2?userID=${userID}&auth=${auth}`, + }); + + return { + status: 'SUCCESS', + decision: data.decision, + }; + } catch (err) { + const error = err; + if (error.response?.status === 404 || error.response.status === 400 || error.response.status === 401) return { status: 'CLIENT_ERROR', decision: 'PRE-DECLINED' }; + return { status: 'SERVER_ERROR', decision: 'PRE-DECLINED' }; + } + } + public static async getPIN(userID: string, auth: string): Promise<{ status: 'SUCCESS' | 'UNAUTHORIZED' | 'PERMISSION_DENIED' | 'CLIENT_ERROR' | 'SERVER_ERROR'; pin?: number[] }> { try { const { data } = await axios({ diff --git a/src/commands/applyt2.ts b/src/commands/applyt2.ts new file mode 100644 index 0000000..93599c0 --- /dev/null +++ b/src/commands/applyt2.ts @@ -0,0 +1,55 @@ +import { Message } from 'eris'; +import { Client, Command, Report, RichEmbed } from '../class'; + +export default class ApplyT2 extends Command { + constructor(client: Client) { + super(client); + this.name = 'apply-t2'; + this.description = 'Applies for Tier 2.'; + this.enabled = true; + this.guildOnly = true; + this.aliases = ['t2']; + this.usage = `${this.client.config.prefix}apply-t2`; + } + + 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 account = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!account) return this.error(message.channel, 'I can\'t find a CS Account for you.'); + if (account.tier > 1) return this.error(message.channel, 'You cannot apply for Tier 2 if you already have Tier 2 or higher.'); + + const loading = await this.loading(message.channel, 'Please wait a moment, processing application...'); + + const decision = await Report.tier2(account.userID, this.client.config.internalKey); + if (decision.status !== 'SUCCESS') { + await loading.delete(); + this.success(message.channel, `__**Decision**__\n\n**Status:** ${decision.decision}\n**Processed by:** EDS (A*01)`); + + if (decision.decision === 'APPROVED') { + const tier = await this.client.db.Tier.findOne({ id: 2 }); + if (account.ramLimitNotification !== -1) { + await account.updateOne({ $set: { tier: 2, ramLimitNotification: tier.resourceLimits.ram - 20 } }); + } else { + await account.updateOne({ $set: { tier: 2 } }); + } + const embed = new RichEmbed(); + embed.setTitle('Cloud Account | Tier Change'); + embed.setColor('#0099ff'); + embed.addField('User', `${account.username} | <@${account.userID}>`, true); + embed.addField('Technician', 'SYSTEM', true); + embed.addField('Old Tier -> New Tier', `${account.tier} -> ${args[1]}`, true); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + await this.client.util.sendMessageToUserTerminal(account.username, 'A technician has changed your tier to 2').catch(() => { }); + this.client.createMessage('580950455581147146', { embed }); return this.client.getDMChannel(account.userID).then((channel) => channel.createMessage({ embed })).catch(); + } + return null; + } + await loading.delete(); + return this.success(message.channel, `__**Decision**__\n\n**Status:** ${decision.decision}\n**Processed by:** EDS (A*01)\n\n\n*Pre-Declines will not result in a hard pull, and they may be due to a server issue or insufficient information. You may want to contact a Staff member for further information.`); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 26be518..cd43cb2 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,5 +1,6 @@ export { default as addreferral } from './addreferral'; export { default as announce } from './announce'; +export { default as applyt2 } from './applyt2'; export { default as authreferral } from './authreferral'; export { default as bearer } from './bearer'; export { default as cloudflare } from './cloudflare';