2021-04-12 20:05:30 -04:00
import { Message , TextChannel } from 'eris' ;
2021-12-23 22:32:51 -05:00
import axios from 'axios' ;
2021-04-12 20:05:30 -04:00
import { apply as Apply } from '.' ;
import { Client , Command , RichEmbed } from '../class' ;
export default class SAA_Approve extends Command {
public applyCommand : Apply ;
constructor ( client : Client ) {
super ( client ) ;
this . name = 'approve' ;
this . description = 'Approves a Staff-assisted Application (SAA).' ;
this . usage = ` ${ this . client . config . prefix } saa approve <applicationID> ` ;
this . permissions = 4 ;
this . guildOnly = true ;
this . enabled = true ;
this . applyCommand = < Apply > this . client . commands . get ( 'apply' ) ;
}
public async run ( message : Message , args : string [ ] ) {
try {
if ( ! args [ 0 ] ) return this . client . commands . get ( 'help' ) . run ( message , [ this . name ] ) ;
2022-03-01 12:18:21 -05:00
const saa = await this . client . db . mongo . SAA . findOne ( { applicationID : args [ 0 ] } ) . lean ( ) . exec ( ) ;
2021-04-12 20:05:30 -04:00
if ( ! saa ) return this . error ( message . channel , 'Unable to locate SAA.' ) ;
const member = this . client . util . resolveMember ( saa . userID , this . mainGuild ) ;
if ( ! member ) return this . error ( message . channel , 'Unable to locate member.' ) ;
2022-03-01 12:18:21 -05:00
const report = await this . client . db . mongo . Score . findOne ( { userID : saa.userID } ) . lean ( ) . exec ( ) ;
2021-04-12 20:05:30 -04:00
if ( ! report ) return this . error ( message . channel , 'Unable to locate Community Report.' ) ;
2022-03-01 12:18:21 -05:00
const staff = await this . client . db . mongo . Staff . findOne ( { userID : message.author.id } ) . lean ( ) . exec ( ) ;
2021-04-12 20:05:30 -04:00
if ( ! staff ) return this . error ( message . channel , 'Unable to locate Staff information.' ) ;
await this . applyCommand . services . get ( saa . serviceCode ) . func ( this . client , member . id ) ;
const embed = new RichEmbed ( ) ;
embed . setTitle ( 'Staff Assisted Application - Decision' ) ;
embed . setColor ( '#50c878' ) ;
// eslint-disable-next-line no-useless-escape
embed . addField ( 'User' , ` ${ member . username } # ${ member . discriminator } | XXX-XX- ${ report . pin [ 2 ] } ` ) ;
embed . addField ( 'Decision' , 'APPROVED' ) ;
embed . addField ( 'Underwriter' , ` ${ message . author . username } , ${ staff . pn . slice ( 0 , 2 ) . join ( ', ' ) } *on behalf of Library of Code sp-us* ` ) ;
embed . addField ( 'Application ID' , saa . applicationID ) ;
embed . addField ( 'Service Code' , saa . serviceCode ) ;
embed . setFooter ( this . client . user . username , this . client . user . avatarURL ) ;
embed . setTimestamp ( ) ;
const log = < TextChannel > this . client . guilds . get ( this . client . config . guildID ) . channels . get ( '611584771356622849' ) ;
log . createMessage ( { embed } ) ;
embed . setDescription ( ` *A SAA has been ran in your name for a service, this embed contains the information about the result of that decision.* \ n \ nAccess link to the EDS recommendation for this decision: https://eds.libraryofcode.org/dec/ ${ saa . edsToken } ` ) ;
const chan = await this . client . getDMChannel ( saa . userID ) ;
chan . createMessage ( { embed } ) . then ( ( ) = > this . success ( message . channel , 'SAA successfully processed.' ) ) . catch ( ( ) = > this . error ( message . channel , 'Unable to deliver decision to user.' ) ) ;
2022-03-01 12:18:21 -05:00
try {
await axios ( {
method : 'PATCH' ,
url : ` https://eds.libraryofcode.org/dec/ ${ saa . applicationID } ?auth= ${ this . client . config . internalKey } ` ,
data : { status : true } ,
} ) ;
} catch ( e ) {
this . error ( message . channel , ` An error occurred while changing EDS data: ${ e } ` ) ;
}
2021-12-23 22:36:13 -05:00
2022-03-01 12:18:21 -05:00
await this . client . db . mongo . SAA . deleteOne ( { _id : saa._id } ) . lean ( ) . exec ( ) ;
2021-04-12 20:05:30 -04:00
} catch ( err ) {
return this . client . util . handleError ( err , message , this ) ;
}
}
}