(info) This AI is still in early stages of development.

Conversation Handler Functions

How to define a conversation handler function....

 

Syntax

The basic syntax is identical to that of a Command Handler Function:

var myHandler = function myHandler(command, player, message, cdo) {
	// your code here
 
	// return a string or array of strings
	return "Command was: "+command;
}

Parameters

ParameterTypeNotesChat Ver
commandString

The command that was issued.

You can create a single handler function and use it as the handler for multiple chat commands, switching internally on the command name.

(warning) Will always be converted to lowercase.

0.1
playerNumber

The ID of the player that issued the command.

This is particularly useful if you want to start a chat conversation with that player in response to the command they just issued.

0.1
messageArray of String

An array containing each word in the player's message.

The string at message[0] will always be the command name (same as command parameter).

If you want the full message as a single string, you can get it from: cdo.message

0.1
cdoConversation Descriptor ObjectThe conversation object associated with the player.0.1

Return Values

ValueTypeNotesChat Ver
<string>StringA single line chat message to send back to the player.0.1
<array>Array of StringA multi-line chat message to send back to the player.0.1
undefinedUndefinedIf your handler function returns nothing, no message will be sent back to the player.0.1
<error>ErrorIf an exception is thrown, the chat API will convert it in to a single line message and send back to the player.0.1

Example

When the user issues the "help" command, a conversation will be started. The conversation re-uses the same helpHandler function, which will provide more help if the user now types 'foo' or 'bar'...

Start conversation when user issues "help" command
var helpHandler = function helpHandler(command, player, message, cdo) {
  switch (command) {
	case "help": {
      // did user already state which help topic?
      if (message[1] == "foo") return helpHandler("foo", player, message);
      if (message[1] == "bar") return helpHandler("bar", player, message);
      // start conversation to find out what topic they want help with...
      chat.start(player, helpHandler); // activate conversation mode
      return [
        "What do you want help with?",
        "Reply '.foo' or '.bar', or '.stop' to abort."
      ];
    }
    case "foo" : {
      chat.stop(player); // chat returns to command mode
      return "some help for foo";
    }
    case "bar" : {
      chat.stop(player); // chat returns to command mode
      return "some help for bar";
    }
    case "stop": {
      chat.stop(player); // chat returns to command mode
      return;
    }
    default    : {
      chat.extend(player); // give user another 15 seconds to reply
      return [
        "What do you want help with?",
        "Reply '.foo' or '.bar', or '.stop' to abort."
      ];
    }
  }
}
 
chat.on("help", helpHandler);
Availability BETA

Requires:

Contents

Jump to:

Chat API

Topics: