2020-10-31 02:27:41 -04:00
import type { AxiosError , AxiosStatic } from 'axios' ;
2020-10-30 22:33:21 -04:00
import axios from 'axios' ;
import { Member , Message } from 'eris' ;
import { Client , Command , RichEmbed } from '../class' ;
export default class Apply extends Command {
2020-10-31 02:27:41 -04:00
public services : Map < string , { description : string , type : ' HARD ' | ' SOFT ' , url : string , validation : ( ...cond : any ) = > Promise < boolean > | boolean , func? : Function } > ;
2020-10-30 22:33:21 -04:00
constructor ( client : Client ) {
super ( client ) ;
this . name = 'apply' ;
this . description = 'apply' ;
this . usage = ` ${ this . client . config . prefix } apply [serviceName] ` ;
this . permissions = 0 ;
this . guildOnly = true ;
this . enabled = true ;
this . setServices ( ) ;
}
protected setServices() {
this . services = new Map ( ) ;
this . services . set ( 'role::constants' , {
description : 'Constants role assignment.' ,
2020-10-31 02:27:41 -04:00
type : 'HARD' ,
2020-10-30 22:33:21 -04:00
url : 'https://eds.libraryofcode.org/roles/constants' ,
validation : ( member : Member ) = > ! member . roles . includes ( '511771731891847168' ) ,
func : async ( client : Client , . . . data : any [ ] ) = > {
2020-10-31 02:27:41 -04:00
const member = await client . guilds . get ( client . config . guildID ) . getRESTMember ( data [ 0 ] ) ;
await member . addRole ( '511771731891847168' , 'Constants Approval from EDS' ) ;
2020-10-30 22:33:21 -04:00
} ,
} ) ;
2020-10-31 02:27:41 -04:00
this . services . set ( 'cs::t2' , {
description : 'Tier 2 upgrade for Cloud Services account.' ,
type : 'HARD' ,
url : 'https://eds.libraryofcode.org/cs/t2' ,
validation : ( member : Member ) = > ! member . roles . includes ( '546457886440685578' ) ,
func : async ( client : Client , . . . data : any [ ] ) = > {
const member = await client . guilds . get ( client . config . guildID ) . getRESTMember ( data [ 0 ] ) ;
const ax = < AxiosStatic > require ( 'axios' ) ;
await ax ( {
method : 'get' ,
url : ` https://api.libraryofcode.org/wh/t2?userID= ${ member . id } &auth= ${ client . config . internalKey } ` ,
} ) ;
} ,
} ) ;
/ * t h i s . s e r v i c e s . s e t ( ' c s : : t 2 ' , {
description : 'Pre-approval for Tier 2.' ,
type : 'SOFT' ,
url : 'https://eds.libraryofcode.org/cs/t2pre-get' ,
validation : ( member : Member ) = > ! member . roles . includes ( '546457886440685578' ) ,
} ) ;
* /
2020-10-30 22:33:21 -04:00
}
public async run ( message : Message , args : string [ ] ) {
try {
if ( ! args [ 0 ] ) {
const embed = new RichEmbed ( ) ;
embed . setTitle ( 'Available Instant Applications' ) ;
for ( const service of this . services ) {
2020-10-31 02:27:41 -04:00
embed . addField ( service [ 0 ] , ` __**Description**__ \ n ${ service [ 1 ] . description } \ n__**Inquiry Type:**__ \ n ${ service [ 1 ] . type } \ n \ n*Run \` ${ this . client . config . prefix } apply ${ service [ 0 ] } \` to apply.* ` , true ) ;
2020-10-30 22:33:21 -04:00
}
embed . setFooter ( this . client . user . username , this . client . user . avatarURL ) ;
embed . setTimestamp ( ) ;
return message . channel . createMessage ( { embed } ) ;
}
if ( ! this . services . has ( args [ 0 ] ) ) return this . error ( message . channel , 'Invalid service/product name.' ) ;
const service = this . services . get ( args [ 0 ] ) ;
const test = await this . services . get ( args [ 0 ] ) . validation ( message . member ) ;
if ( ! test ) return this . error ( message . channel , 'A condition exists which prevents you from applying, please try again later.' ) ;
const msg = await this . loading ( message . channel , 'Thank you for submitting an application. We are currently processing it, you will be pinged here shortly with the decision.' ) ;
return await this . client . queue . processApplication ( { channelID : message.channel.id , guildID : this.mainGuild.id , messageID : msg.id } , service . url , message . author . id , service . func . toString ( ) ) ;
} catch ( err ) {
return this . client . util . handleError ( err , message , this ) ;
}
}
public static async apply ( client : Client , url : string , userID : string ) {
try {
const { data } = await axios ( {
method : 'get' ,
url : ` ${ url } ?userID= ${ userID } &auth= ${ client . config . internalKey } ` ,
} ) ;
return {
status : 'SUCCESS' ,
decision : data.decision ,
} ;
} catch ( err ) {
const error = < AxiosError > 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' } ;
}
}
}