This AI is still in early stages of development.

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

How to define a command handler function....

 

Syntax

var myHandler = function myHandler(command, player, message, conversation) {
	// 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.

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).

0.1
conversationConversation objectThe conversation object associated with '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

Single command, with different output based on second keyword
var helpHandler = function helpHandler(command, player, message) {
  // command == message[0] == "help"
  // message[1] == second keyword (eg. 'foo' if user sent '.help foo')
  switch (message[1]) {
    case "foo": return "some help for foo";
    case "bar": return "some help for bar";
    default   : return "Type '.help <whatever>' where <whatever> can be either 'foo' or 'bar'";
  }
}
 
registerChatCommand("help", helpHandler);
 
/* user can now send commands such as:
 
.help = Type '.help <whatever>' where <whatever> can be either 'foo' or 'bar'
.help foo = some help for foo
.help bar = some help for bar
*/
Single handler for multiple commands
var multiHandler = function multiHandler(command) {
  switch (command) {
    case "foo": return "command: foo";
    case "bar": return "command: bar";
    default   : return "command: "+command+" (which I don't understand)";
  }
}
 
registerChatCommand("foo", multiHnadler); // .foo = command: foo
registerChatCommand("bar", multiHandler); // .bar = command: bar
registerChatCommand("moo", multiHandler); // .moo = command: moo (which I don't understand)

Availability BETA

Requires:

Chat API

Topics:

 

  • No labels