2020-04-16 22:56:52 -04:00
/* eslint-disable no-bitwise */
2020-04-14 21:33:34 -04:00
import signale from 'signale' ;
2020-04-15 12:19:46 -04:00
import { Member , Message , Guild , PrivateChannel , GroupChannel , Role , AnyGuildChannel } from 'eris' ;
2020-04-15 15:13:44 -04:00
import { Client , Command , Moderation , RichEmbed } from '.' ;
2020-04-14 21:18:44 -04:00
import { statusMessages as emotes } from '../configs/emotes.json' ;
2020-04-14 13:15:33 -04:00
export default class Util {
public client : Client ;
2020-04-15 15:13:44 -04:00
public moderation : Moderation ;
2020-04-14 21:33:34 -04:00
public signale : signale.Signale ;
2020-04-14 13:15:33 -04:00
constructor ( client : Client ) {
this . client = client ;
2020-04-15 15:13:44 -04:00
this . moderation = new Moderation ( this . client ) ;
2020-04-14 21:33:34 -04:00
this . signale = signale ;
this . signale . config ( {
displayDate : true ,
displayTimestamp : true ,
displayFilename : true ,
} ) ;
2020-04-14 13:15:33 -04:00
}
get emojis() {
return {
2020-04-14 21:18:44 -04:00
SUCCESS : emotes.success ,
LOADING : emotes.loading ,
ERROR : emotes.error ,
2020-04-14 13:15:33 -04:00
} ;
}
/ * *
* Resolves a command
* @param query Command input
* @param message Only used to check for errors
* /
public resolveCommand ( query : string | string [ ] ) : Promise < { cmd : Command , args : string [ ] } > {
try {
// let resolvedCommand: Command;
// eslint-disable-next-line no-param-reassign
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-04-14 19:03:10 -04:00
2020-04-16 22:56:52 -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 ( ) ) ) ;
2020-04-15 12:19:46 -04:00
}
2020-04-15 11:57:37 -04:00
public resolveRole ( query : string , { roles } : Guild ) : Role | undefined {
2020-04-16 22:56:52 -04:00
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 ( ) ) ) ;
2020-04-15 11:57:37 -04:00
}
2020-04-15 11:15:50 -04:00
public resolveMember ( query : string , { members } : Guild ) : Member | undefined {
2020-04-16 22:56:52 -04:00
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 ( ) ) ) ) ;
2020-04-14 19:03:10 -04:00
}
2020-04-14 21:33:34 -04:00
2020-05-06 19:32:51 -04:00
public async handleError ( error : Error , message? : Message , command? : Command , disable = true ) : Promise < void > {
2020-04-14 21:33:34 -04:00
try {
this . signale . error ( error ) ;
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 || 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 ) ) ;
info . embed = embed ;
}
await this . client . createMessage ( '595788220764127272' , info ) ;
2020-04-16 22:56:52 -04:00
const msg = message ? message . content . slice ( this . client . config . prefix . length ) . trim ( ) . split ( / +/g ) : [ ] ;
2020-04-14 21:33:34 -04:00
// eslint-disable-next-line no-param-reassign
2020-04-15 15:33:27 -04:00
if ( command && disable ) this . resolveCommand ( msg ) . then ( ( c ) = > { c . cmd . enabled = false ; } ) ;
2020-05-06 19:32:51 -04:00
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.' : '' } *** ` ) ;
2020-04-14 21:33:34 -04:00
} catch ( err ) {
this . signale . error ( err ) ;
}
}
2020-04-15 15:13:44 -04:00
public splitString ( string : string , length : number ) : string [ ] {
if ( ! string ) return [ ] ;
// eslint-disable-next-line no-param-reassign
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 ) ;
// eslint-disable-next-line no-param-reassign
string = string . substr ( pos ) ;
arrayString . push ( str ) ;
}
return arrayString ;
}
2020-04-16 22:56:52 -04:00
2020-05-01 01:46:13 -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 ;
}
2020-04-16 22:56:52 -04:00
public decimalToHex ( int : number ) : string {
2020-04-21 13:53:12 -04:00
const hex = int . toString ( 16 ) ;
return '#000000' . substring ( 0 , 7 - hex . length ) + hex ;
2020-04-16 22:56:52 -04:00
}
2020-04-14 13:15:33 -04:00
}