file description import type { Message } from "eris" ; import type { HibikiClient } from "./Client" ; import type { ArgTypes } from "../typings/argtypes" ; import { getRESTUser } from "../utils/getRESTUser" ; export class Args { argtypes : ArgTypes ; bot : HibikiClient ; constructor ( bot ) { this . bot = bot ; this . argtypes = { boolean : ( a , _msg , flag ) => { if ( a === "on" || a === "true" || a === "enable" || ( flag ?. includes ( "strict" ) ? a === "yes" : a . startsWith ( "y" ) ) ) return true ; if ( a === "off" || a === "false" || a === "disable" || ( flag ?. includes ( "strict" ) ? a === "no" : a . startsWith ( "n" ) ) ) return false ; } , channel : ( a , msg , flag ) => { const channel = msg . channel . guild . channels . find ( ( c ) => c . id === a || a . startsWith ( `<#${ c . id }>` ) || c . name . startsWith ( a ) ) ; if ( channel ?. type === 4 || ( channel ?. type === 2 && flag ?. includes ( "allowVoice" ) ) ) return ; if ( ! channel && flag ?. includes ( "fallback" ) ) return msg . channel ; return channel ; } , channelArray : ( a , msg ) => { const channels = [ ] ; a . forEach ( ( i ) => { const channel = msg . channel . guild . channels . find ( ( c ) => c . name . toLowerCase ( ) . startsWith ( i . toLowerCase ( ) ) || c . id === i || i === `<#${ c . id }>` ) ; if ( ! channel ) return ; if ( channel ?. type !== 0 ) return ; channels . push ( channel . id ) ; } ) ; if ( ! channels . length ) return "No channels" ; return channels ; } , guild : ( a , _msg , flag , b ) => { return b . guilds . find ( ( g ) => g . id === a || ( flag ?. includes ( "strict" ) ? g . name === a : g . name . startsWith ( a ) ) ) ; } , member : ( a , msg , flag ) => { const member = msg . channel . guild ?. members ?. find ( ( m ) => ! flag ?. includes ( "strict" ) ? m . user . username . toLowerCase ( ) === a || m . id === a || a ?. startsWith ( `<@!${ m . id }>` ) || a ?. startsWith ( `<@${ m . id }>` ) || m . nick ?. toLowerCase ( ) === a || m . user . username . startsWith ( a ) : m . id === a || a ?. startsWith ( `<@!${ m . id }>` ) || a ?. startsWith ( `<@${ m . id }>` ) ) ; if ( ! member && flag ?. includes ( "userFallback" ) ) return this . argtypes . user ( a , msg , [ "REST" ] ) ; if ( ( ! a || ! member ) && flag ?. includes ( "fallback" ) ) return msg . channel . guild ?. members ?. get ( msg . author . id ) ; if ( ! flag && member ?. id === msg . author . id ) return ; return member ; } , number : ( a , _msg , flag ) => { if ( isNaN ( a ) ) return ; if ( flag ?. includes ( "negative" ) ) return a ; if ( a < 0 ) return ; return a ; } , role : ( a , msg ) => { const role = msg . channel . guild . roles . find ( ( r ) => ( r . id === a || a . startsWith ( `<@&${ r . id }>` ) || r . name . toLowerCase ( ) . startsWith ( a . toLowerCase ( ) ) ) && ! r . managed ) ; return role ; } , roleArray : ( a , msg ) => { const roles = [ ] ; a . forEach ( ( i ) => { const role = msg . channel . guild . roles . find ( ( r ) => ( r . name . toLowerCase ( ) . startsWith ( i . toLowerCase ( ) ) || r . id === i || i === `<@&${ r . id }>` ) && ! r . managed ) ; if ( ! role ) return ; roles . push ( role . id ) ; } ) ; return roles ; } , string : ( a ) => { return a ; } , user : async ( a , _msg , flag ) => { let user = this . bot . users . find ( ( u ) => u . id === a || a ?. startsWith ( `<@!${ u . id }>` ) || a ?. startsWith ( `<@${ u . id }>` ) ) ; if ( ! user ?. id && flag ?. includes ( "REST" ) ) user = await getRESTUser ( a , this . bot ) ; if ( ! user ?. id ) return ; return user ; } , voiceChannel : ( a , msg ) => { const channel = msg . channel . guild . channels . find ( ( c ) => c . id === a || a . startsWith ( `<#${ c . id }>` ) || c . name . startsWith ( a ) ) ; if ( ! channel || channel ?. type !== 2 ) return ; return channel ; } , } ; } async parse ( argString , args , msg ) { const argObj = [ ] ; argString . split ( " " ) . forEach ( ( arg ) => { const argumentRegex = /(<|\[)(\w{1,}):(\w{1,})&?([\w=*,]{1,})?(>|\])/ . exec ( arg ) ; if ( ! argumentRegex ) return ; argObj . push ( { name : argumentRegex [ 2 ] , type : argumentRegex [ 3 ] , flag : argumentRegex [ 4 ] ?. split ( "," ) , optional : argumentRegex [ 1 ] === "[" , } ) ; } ) ; args . split ( " " ) . forEach ( ( arg , i ) => { const argument = argObj [ i ] ; if ( ! argument || ( ! arg && ! argument . flag ?. includes ( "fallback" ) ) ) return ; if ( ! this . argtypes [ argument . type ] ) return ; const value = this . argtypes [ argument . type ] ( arg . toLowerCase ( ) , msg , argument . flag , this . bot ) ; if ( typeof value == "undefined" ) return ; argument . value = value ; argObj [ i ] = argument ; } ) ; return argObj ; } }