2019-10-14 15:46:10 -04:00
import { promisify } from 'util' ;
import childProcess from 'child_process' ;
import nodemailer from 'nodemailer' ;
2019-10-19 09:31:54 -04:00
import { Message , TextChannel , PrivateChannel } from 'eris' ;
2019-10-15 20:34:13 -04:00
import { Client } from '.' ;
2019-10-19 09:31:54 -04:00
import { Command , RichEmbed } from './class' ;
2019-10-14 15:46:10 -04:00
export default class Util {
2019-10-14 23:32:37 -04:00
public client : Client ;
constructor ( client : Client ) {
this . client = client ;
}
2019-10-14 15:46:10 -04:00
public async exec ( command : string ) : Promise < string > {
const ex = promisify ( childProcess . exec ) ;
let result : string ;
2019-10-15 18:44:46 -04:00
// eslint-disable-next-line no-useless-catch
2019-10-14 15:46:10 -04:00
try {
const res = await ex ( command ) ;
2019-10-15 18:44:46 -04:00
result = res . stderr || res . stdout ;
2019-10-14 15:46:10 -04:00
} catch ( err ) {
throw err ;
}
return result ;
}
2019-10-14 23:32:37 -04:00
2019-10-15 10:42:42 -04:00
public resolveCommand ( command : string ) : Command {
if ( this . client . commands . has ( command ) ) return this . client . commands . get ( command ) ;
for ( const cmd of this . client . commands . values ( ) ) {
2019-10-15 18:44:46 -04:00
if ( ! cmd . aliases ) continue ; // eslint-disable-line no-continue
2019-10-14 19:04:07 -04:00
for ( const alias of cmd . aliases ) {
2019-10-14 23:37:04 -04:00
if ( command === alias . toLowerCase ( ) ) return cmd ;
2019-10-14 19:04:07 -04:00
}
}
2019-10-15 18:44:46 -04:00
return undefined ;
2019-10-14 19:04:07 -04:00
}
2019-10-15 19:10:37 -04:00
2019-10-19 09:31:54 -04:00
public async handleError ( error : Error , message? : Message , command? : Command ) : Promise < void > {
const info = { content : ` \` \` \` js \ n ${ error . stack } \ n \` \` \` ` , embed : null }
if ( message ) {
const embed = new RichEmbed ( ) ;
embed . setColor ( 'FF0000' ) ;
embed . setAuthor ( ` Error caused by ${ message . author . username } # ${ message . author . discriminator } ` , message . author . avatarURL ) ;
embed . setTitle ( 'Message content' ) ;
embed . setDescription ( message . content ) ;
embed . addField ( 'User' , ` ${ message . author . mention } ( \` ${ message . author . id } \` ) ` , true ) ;
embed . addField ( 'Channel' , message . channel . mention , true ) ;
let guild : string ;
if ( message . channel instanceof PrivateChannel ) guild = '@me' ;
else guild = message . channel . guild . id
embed . addField ( 'Message link' , ` [Click here](https://discordapp.com/channels/ ${ guild } / ${ message . channel . id } / ${ message . id } ) ` , true )
embed . setTimestamp ( new Date ( message . timestamp ) ) ;
info . embed = embed ;
}
await this . client . createMessage ( '595788220764127272' , info ) ;
if ( message ) this . client . createMessage ( '595788220764127272' , ` Message content for above error ` )
2019-10-15 19:10:37 -04:00
if ( command ) this . client . commands . get ( command . name ) . enabled = false ;
if ( message ) message . channel . createMessage ( ` *** ${ this . client . stores . emojis . error } An unexpected error has occured - please contact a member of the Engineering Team. ${ command ? ' This command has been disabled.' : '' } *** ` ) ;
}
2019-10-19 07:47:08 -04:00
public splitFields ( fields : { name : string , value : string , inline? : boolean } [ ] ) : { name : string , value : string , inline? : boolean } [ ] [ ] {
let index = 0 ;
const array : { name : string , value : string , inline? : boolean } [ ] [ ] = [ [ ] ] ;
while ( fields . length ) {
if ( array [ index ] . length >= 25 ) { index += 1 ; array [ index ] = [ ] ; }
array [ index ] . push ( fields [ 0 ] ) ; fields . shift ( ) ;
}
return array ;
}
2019-10-14 23:32:37 -04:00
}