Add example of stopping an input modifier

This commit is contained in:
Andrew Cantino 2020-09-28 15:32:05 -07:00
parent ddf4d627c0
commit 7e26f0d756
2 changed files with 28 additions and 1 deletions

26
examples/commandParser.js Normal file
View file

@ -0,0 +1,26 @@
// This is an example Input Modifier that looks for commands from the user.
const modifier = (text) => {
let stop = false
// This matches when the user types in ":something arg1 arg2" in any of the three input formats. For example, they could
// type ":status" and then command would be "status" and args would be [], or they could type ":walk north" and command
// would be "walk" and args would be ["north"].
const commandMatcher = text.match(/\n? ?(?:> You |> You say "|):(\w+?)( [\w ]+)?[".]?\n?$/i)
if (commandMatcher) {
const command = commandMatcher[1]
const args = commandMatcher[2] ? commandMatcher[2].trim().split(' ') : []
state.message = `Got command '${command}' with args ${JSON.stringify(args)}`
stop = true
} else {
delete state.message
}
// You must return an object with the text property defined.
// If you include { stop: true } when inside of an input modifier, processing will be stopped and nothing will be
// sent to the AI.
return { text, stop }
}
// Don't modify this part
modifier(text)