import { ChanLayer , chanLayerFromString } from '../chanLayer' ; export interface CmdEntry { cmd : string ; fn : ( chanLayer : ChanLayer , params : string [ ] ) => Promise < boolean > ; } export interface CmdSet { group : string ; entries : CmdEntry [ ] ; } export interface CmdList { list ( ) : CmdSet ; } export class Commands { private readonly map : CmdSet [ ] ; constructor ( ) { this . map = [ ] ; } add ( entries ) { this . map . push ( entries ) ; } async process ( command ) { let result = false ; let cmdIndex = 2 ; let group = this . map . find ( ( { group } ) => group === command [ 0 ] . toUpperCase ( ) ) ; if ( ! group ) { group = this . map . find ( ( { group } ) => group === '' ) ; cmdIndex = 0 ; } if ( group ) { const entry = group . entries . find ( ( { cmd } ) => cmd === command [ cmdIndex ] . toUpperCase ( ) ) ; if ( entry ) { const chanLayer = chanLayerFromString ( command [ 1 ] ) ; result = await entry . fn ( chanLayer , command . slice ( cmdIndex == 2 ? 3 : 2 ) ) ; } } return result ; } }