2020-09-21 02:53:18 -04:00
/* eslint-disable default-case */
import jwt from 'jsonwebtoken' ;
import { Message } from 'eris' ;
import { Client , Command } from '../class' ;
2020-11-01 04:32:24 -05:00
export default class Offer extends Command {
2020-09-21 02:53:18 -04:00
constructor ( client : Client ) {
super ( client ) ;
this . name = 'offer' ;
2020-11-01 04:32:24 -05:00
this . description = 'Pre-qualifies a member for an offer. Will run a hard-pull automatically on acceptance.' ;
2020-09-21 02:53:18 -04:00
this . usage = ` ${ this . client . config . prefix } offer <member> <name of offer>:<department/service for hard pull> ` ;
this . permissions = 4 ;
this . aliases = [ 'qualify' ] ;
this . guildOnly = true ;
this . enabled = true ;
}
public async run ( message : Message , args : string [ ] ) {
try {
if ( ! args [ 0 ] ) return this . client . commands . get ( 'help' ) . run ( message , [ this . name ] ) ;
const member = this . client . util . resolveMember ( args [ 0 ] , this . mainGuild ) ;
if ( ! member ) return this . error ( message . channel , 'Could not find member.' ) ;
const score = await this . client . db . Score . findOne ( { userID : member.user.id } ) . lean ( ) . exec ( ) ;
if ( ! score ) return this . error ( message . channel , 'Could not find score report for this user.' ) ;
2020-09-21 03:33:22 -04:00
if ( score . locked ) return this . error ( message . channel , 'This user\'s score report is locked.' ) ;
2020-09-21 02:53:18 -04:00
const name = args . slice ( 1 ) . join ( ' ' ) . split ( ':' ) [ 0 ] ;
const dept = args . slice ( 1 ) . join ( ' ' ) . split ( ':' ) [ 1 ] ;
if ( ! name || ! dept ) return this . error ( message . channel , 'Invalid arguments.' ) ;
const token = jwt . sign ( {
userID : member.user.id ,
staffID : message.author.id ,
channelID : message.channel.id ,
messageID : message.id ,
pin : score.pin.join ( '-' ) ,
name ,
department : dept ,
date : new Date ( ) ,
2020-09-21 03:33:22 -04:00
} , this . client . config . internalKey , { expiresIn : '12h' } ) ;
this . client . getDMChannel ( member . user . id ) . then ( ( chan ) = > {
2020-11-01 04:32:24 -05:00
chan . createMessage ( ` __**Offer Pre-Qualified**__ \ nYou have been pre-approved for an offer! If you wish to accept this offer, please enter the offer code at https://report.libraryofcode.org/. Do not share this code with anyone else. This offer automatically expires in 12 hours. Your report will be Hard Inquiried immediately after accepting this offer. \ n \ n**Department:** ${ dept . toUpperCase ( ) } \ n**Offer for:** ${ name } \ n \ n \` ${ token } \` ` ) ;
2020-09-21 02:53:18 -04:00
} ) . catch ( ( ) = > this . error ( message . channel , 'Could not DM member.' ) ) ;
return this . success ( message . channel , ` Offer sent. \ n \ n \` ${ token } \` ` ) ;
} catch ( err ) {
return this . client . util . handleError ( err , message , this ) ;
}
}
}