slashcmd
Version:
A minimal slash-command parser.
1 lines • 736 B
JavaScript
const errors=["Expected '${c}' to be before command at 1:1.","Expected command at ${l}, got nonexistent command."];class Commands{static #n=new Map();static #t='/';static registerCommand(n){this.#n.set(n.name,n)}static deregisterCommand(l){this.#n.delete(l.name)}static setInitializer(i){this.#t=i}static execute(c){if(!c.startsWith(this.#t))throw new TypeError(errors[0].replace("${c}",this.#t));let p=c.slice(this.#t.length).split(" ");let u=p[0];let a=p.slice(1);const f=this.#n.get(u);if(f){try{f.execute(a,(b)=>{throw new Error(b);},errors);}catch(e){throw e};}else throw new TypeError(errors[1].replace("${l}","1:"+u.length));}}if(typeof module!=='undefined'&&typeof module.exports!=='undefined'){module.exports={Commands,errors}}