2020-07-06 03:15:06 -04:00
/* eslint-disable no-bitwise */
import nodemailer from 'nodemailer' ;
2020-11-30 19:51:20 -05:00
import GoogleTTS , { TextToSpeechClient } from '@google-cloud/text-to-speech' ;
import childProcess from 'child_process' ;
import ARIClient from 'ari-client' ;
2020-12-02 00:26:46 -05:00
import AMIClient from 'asterisk-manager' ;
2020-07-06 03:15:06 -04:00
import signale from 'signale' ;
2020-07-15 10:51:50 -04:00
import { Member , Message , Guild , PrivateChannel , GroupChannel , Role , AnyGuildChannel , WebhookPayload } from 'eris' ;
2020-12-01 15:25:18 -05:00
import { Client , Command , Moderation , PBX , RichEmbed } from '.' ;
2020-07-06 03:15:06 -04:00
import { statusMessages as emotes } from '../configs/emotes.json' ;
export default class Util {
public client : Client ;
public moderation : Moderation ;
public signale : signale.Signale ;
public transporter : nodemailer.Transporter ;
2020-12-01 15:25:18 -05:00
public pbx : PBX ;
2020-11-30 19:51:20 -05:00
public ari : ARIClient.Client ;
2020-12-02 00:26:46 -05:00
public ami : any ;
2020-11-30 19:51:20 -05:00
public tts : TextToSpeechClient ;
2020-07-06 03:15:06 -04:00
constructor ( client : Client ) {
this . client = client ;
this . moderation = new Moderation ( this . client ) ;
this . signale = signale ;
this . signale . config ( {
displayDate : true ,
displayTimestamp : true ,
displayFilename : true ,
} ) ;
this . transporter = nodemailer . createTransport ( {
host : 'staff.libraryofcode.org' ,
port : 587 ,
auth : { user : 'internal' , pass : this.client.config.emailPass } ,
} ) ;
2020-11-30 19:51:20 -05:00
this . load ( ) ;
}
private async load() {
this . ari = await ARIClient . connect ( 'http://10.8.0.1:8088/ari' , 'cr0' , this . client . config . ariClientKey ) ;
2020-12-01 22:27:35 -05:00
this . ari . start ( [ 'cr-zero' , 'page-dtmf' ] ) ;
2020-11-30 19:51:20 -05:00
2020-12-02 00:26:46 -05:00
this . ami = new AMIClient ( 5038 , '10.8.0.1' , this . client . config . amiClientKey ) ;
2020-11-30 19:51:20 -05:00
process . env . GOOGLE_APPLICATION_CREDENTIALS = ` ${ __dirname } /../../google.json ` ;
this . tts = new GoogleTTS . TextToSpeechClient ( ) ;
2020-12-01 15:25:18 -05:00
this . pbx = new PBX ( this . client ) ;
2020-07-06 03:15:06 -04:00
}
get emojis() {
return {
SUCCESS : emotes.success ,
LOADING : emotes.loading ,
ERROR : emotes.error ,
} ;
}
/ * *
* Resolves a command
* @param query Command input
* @param message Only used to check for errors
* /
2020-11-07 04:30:53 -05:00
/ * p u b l i c r e s o l v e C o m m a n d ( q u e r y : s t r i n g | s t r i n g [ ] ) : P r o m i s e < { c m d : C o m m a n d , a r g s : s t r i n g [ ] } > {
2020-07-06 03:15:06 -04:00
try {
if ( typeof query === 'string' ) query = query . split ( ' ' ) ;
const commands = this . client . commands . toArray ( ) ;
const resolvedCommand = commands . find ( ( c ) = > c . name === query [ 0 ] . toLowerCase ( ) || c . aliases . includes ( query [ 0 ] . toLowerCase ( ) ) ) ;
if ( ! resolvedCommand ) return Promise . resolve ( null ) ;
query . shift ( ) ;
return Promise . resolve ( { cmd : resolvedCommand , args : query } ) ;
} catch ( error ) {
return Promise . reject ( error ) ;
}
}
2020-11-07 04:30:53 -05:00
* /
2020-11-30 19:51:20 -05:00
public async exec ( command : string , options : childProcess.ExecOptions = { } ) : Promise < string > {
return new Promise ( ( res , rej ) = > {
let output = '' ;
const writeFunction = ( data : string | Buffer | Error ) = > {
output += ` ${ data } ` ;
} ;
const cmd = childProcess . exec ( command , options ) ;
cmd . stdout . on ( 'data' , writeFunction ) ;
cmd . stderr . on ( 'data' , writeFunction ) ;
cmd . on ( 'error' , writeFunction ) ;
cmd . once ( 'close' , ( code , signal ) = > {
cmd . stdout . off ( 'data' , writeFunction ) ;
cmd . stderr . off ( 'data' , writeFunction ) ;
cmd . off ( 'error' , writeFunction ) ;
setTimeout ( ( ) = > { } , 1000 ) ;
if ( code !== 0 ) rej ( new Error ( ` Command failed: ${ command } \ n ${ output } ` ) ) ;
res ( output ) ;
} ) ;
} ) ;
}
2020-11-07 04:30:53 -05:00
/ * *
* Resolves a command
* @param query Command input
* @param message Only used to check for errors
* /
public resolveCommand ( query : string | string [ ] , message? : Message ) : Promise < { cmd : Command , args : string [ ] } > {
try {
let resolvedCommand : Command ;
if ( typeof query === 'string' ) query = query . split ( ' ' ) ;
const commands = this . client . commands . toArray ( ) ;
resolvedCommand = commands . find ( ( c ) = > c . name === query [ 0 ] . toLowerCase ( ) || c . aliases . includes ( query [ 0 ] . toLowerCase ( ) ) ) ;
if ( ! resolvedCommand ) return Promise . resolve ( null ) ;
query . shift ( ) ;
while ( resolvedCommand . subcommands . size && query . length ) {
const subCommands = resolvedCommand . subcommands . toArray ( ) ;
const found = subCommands . find ( ( c ) = > c . name === query [ 0 ] . toLowerCase ( ) || c . aliases . includes ( query [ 0 ] . toLowerCase ( ) ) ) ;
if ( ! found ) break ;
resolvedCommand = found ;
query . shift ( ) ;
}
return Promise . resolve ( { cmd : resolvedCommand , args : query } ) ;
} catch ( error ) {
if ( message ) this . handleError ( error , message ) ;
else this . handleError ( error ) ;
return Promise . reject ( error ) ;
}
}
2020-07-06 03:15:06 -04:00
public resolveGuildChannel ( query : string , { channels } : Guild , categories = false ) : AnyGuildChannel | undefined {
const ch : AnyGuildChannel [ ] = channels . filter ( ( c ) = > ( ! categories ? c . type !== 4 : true ) ) ;
return ch . find ( ( c ) = > c . id === query . replace ( /[<#>]/g , '' ) || c . name === query )
|| ch . find ( ( c ) = > c . name . toLowerCase ( ) === query . toLowerCase ( ) )
|| ch . find ( ( c ) = > c . name . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) ) ;
}
public resolveRole ( query : string , { roles } : Guild ) : Role | undefined {
return roles . find ( ( r ) = > r . id === query . replace ( /[<@&>]/g , '' ) || r . name === query )
|| roles . find ( ( r ) = > r . name . toLowerCase ( ) === query . toLowerCase ( ) )
|| roles . find ( ( r ) = > r . name . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) ) ;
}
public resolveMember ( query : string , { members } : Guild ) : Member | undefined {
return members . find ( ( m ) = > ` ${ m . username } # ${ m . discriminator } ` === query || m . username === query || m . id === query . replace ( /[<@!>]/g , '' ) || m . nick === query ) // Exact match for mention, username+discrim, username and user ID
|| members . find ( ( m ) = > ` ${ m . username . toLowerCase ( ) } # ${ m . discriminator } ` === query . toLowerCase ( ) || m . username . toLowerCase ( ) === query . toLowerCase ( ) || ( m . nick && m . nick . toLowerCase ( ) === query . toLowerCase ( ) ) ) // Case insensitive match for username+discrim, username
|| members . find ( ( m ) = > m . username . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) || ( m . nick && m . nick . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) ) ) ;
}
public async handleError ( error : Error , message? : Message , command? : Command , disable = true ) : Promise < void > {
try {
this . signale . error ( error ) ;
2020-07-15 10:51:50 -04:00
const info : WebhookPayload = { content : ` \` \` \` js \ n ${ error . stack || error } \ n \` \` \` ` , embeds : [ ] } ;
2020-07-06 03:15:06 -04:00
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 || message . channel instanceof GroupChannel ) 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 ) ) ;
2020-07-15 10:51:50 -04:00
info . embeds . push ( embed ) ;
2020-07-06 03:15:06 -04:00
}
2020-07-15 10:51:50 -04:00
await this . client . executeWebhook ( this . client . config . webhookID , this . client . config . webhookToken , info ) ;
2020-07-06 03:15:06 -04:00
const msg = message ? message . content . slice ( this . client . config . prefix . length ) . trim ( ) . split ( / +/g ) : [ ] ;
if ( command && disable ) this . resolveCommand ( msg ) . then ( ( c ) = > { c . cmd . enabled = false ; } ) ;
if ( message ) message . channel . createMessage ( ` *** ${ this . emojis . ERROR } An unexpected error has occured - please contact a Staff member. ${ command && disable ? ' This command has been disabled.' : '' } *** ` ) ;
} catch ( err ) {
this . signale . error ( err ) ;
}
}
public splitString ( string : string , length : number ) : string [ ] {
if ( ! string ) return [ ] ;
if ( Array . isArray ( string ) ) string = string . join ( '\n' ) ;
if ( string . length <= length ) return [ string ] ;
const arrayString : string [ ] = [ ] ;
let str : string = '' ;
let pos : number ;
while ( string . length > 0 ) {
pos = string . length > length ? string . lastIndexOf ( '\n' , length ) : string . length ;
if ( pos > length ) pos = length ;
str = string . substr ( 0 , pos ) ;
string = string . substr ( pos ) ;
arrayString . push ( str ) ;
}
return arrayString ;
}
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 ;
}
2020-07-13 00:01:53 -04:00
public splitArray < T > ( array : T [ ] , count : number ) {
const finalArray : T [ ] [ ] = [ ] ;
while ( array . length ) {
finalArray . push ( array . splice ( 0 , count ) ) ;
}
return finalArray ;
}
2020-07-06 03:15:06 -04:00
public decimalToHex ( int : number ) : string {
const hex = int . toString ( 16 ) ;
return '#000000' . substring ( 0 , 7 - hex . length ) + hex ;
}
2020-09-19 16:30:44 -04:00
public randomNumber ( min : number , max : number ) : number {
return Math . round ( Math . random ( ) * ( max - min ) + min ) ;
}
2020-09-27 02:11:26 -04:00
public encode ( arg : string ) {
return arg . split ( '' ) . map ( ( x ) = > x . charCodeAt ( 0 ) / 400 ) ;
}
2020-09-29 23:44:20 -04:00
public normalize ( string ) {
const input = [ ] ;
// eslint-disable-next-line no-plusplus
for ( let i = 0 ; i < string . length ; i ++ ) {
input . push ( string . charCodeAt ( i ) / 1000 ) ;
}
return input ;
}
public convert_ascii ( ascii : [ ] ) {
let string = '' ;
// eslint-disable-next-line no-plusplus
for ( let i = 0 ; i < ascii . length ; i ++ ) {
string += String . fromCharCode ( ascii [ i ] * 4000 ) ;
}
return string ;
}
2020-10-06 15:06:17 -04:00
public percentile ( arr : number [ ] , val : number ) {
return ( 100 * arr . reduce ( ( acc , v ) = > acc + ( v < val ? 1 : 0 ) + ( v === val ? 0.5 : 0 ) , 0 ) ) / arr . length ;
}
public ordinal ( i : number ) {
const j = i % 10 ;
const k = i % 100 ;
if ( j === 1 && k !== 11 ) {
return ` ${ i } st ` ;
}
if ( j === 2 && k !== 12 ) {
return ` ${ i } nd ` ;
}
if ( j === 3 && k !== 13 ) {
return ` ${ i } rd ` ;
}
return ` ${ i } th ` ;
}
2020-07-06 03:15:06 -04:00
}