Chat Command
Parse and execute commands from text in a flexible and configurable way. This is intended to be consumed by anything that might want to run arbitrary commands (and provide output) based on text.
(currently) only intended for use in a Node environment.
Setup
npm install -S chat-command
/** * The default export is a higher order function that should be * called with (namespace<string>, actions<object>, overrides<object?>) * This will return a function that executes commands from text * based on the initialization parameters */const chatCommand = 'bot' hello: 'hello' 'goodbye' // ['hello', 'goodbye']
Examples
Call actions with arguments
const chatCommandFactory = const customMathParser = // [3, 10] // You can even use native objects if you're especially adventurousconst nativeMathParser = // [1, 2, 10, 3.141592653589793]
Nest actions to create logical command structures
const players = const chatCommand = 'game' players: { players return `Added ` } { return ` are fighting!` } // ['Added Bob', 'Added Bill', 'Bob and Bill are fighting!']
Handle asynchronous actions
const Database = const chatCommand = 'db' todos: { return { Database } } Promiseall
Override default parsing behavior and/or exclude namespace
const chatCommandFactory = const tildeCommand = '' i: love: tildes: 'Me too!' { return a + b + c } delimiter: '~' // default: '.' argumentDelimiter: '&' // default: /, ?/ // ['Me too!', 'abc'] const slashCommand = // ['I can try...']
The logic is composed of helpers parse
and execute
which may also be accessed separately for more granular control
const parse execute = 'bot' doThis: 'ok' { throw Error'Bad' } // ['ok'] // 'ok'