Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Excerpt |
---|
Some ideas on how to handle multiple dialects... |
Overview
The quick start guide shows one way of dealing with multiple dialects – using switch statements. However, switch statements can be quite bloaty and it's not particularly easy to hive them off in to separate .js files without needing to add lots more cruft to those files.
An alternative approach is to use an object for each dialect, then use some basic JS API tricks to use the functions in those objects. For example, let's say your AI understands several dialects: "ai" (standad dialect), "nb", "cb" and "nx"...
A home for dialects
First, we need somewhere to store our dialect objects...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
include("path/to/APIs/Babel.js"); chat.dialects = {}; |
Ok, that was easy
Dialect objects
Now let's create our first dialect object, let's do the "nb" (NullBot) dialect – it will define the function for each of the NullBot commands we want to implement...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// create an object for the NullBot commands we are implementing... chat.dialects["nb"] = {}; // now let's implement NullBot's "who" command: chat.dilalects.nb["who"] = function(sender, message, data) { // code here // maybe this command should reply with something? return [ "iAm", // message "nb", // dialect {personality: "mc"} // data ]; } // maybe we should handle the "iAm" command too? // in case a nullbot decides to tell us it's personality... chat.dialects.nb["iAm"] = function(sender, message, data) { chat(sender, "I'll research "+data.personality+" too in that case!"); } // and another one... chat.dialects.nb["res machineguns"] = function(sender, message, data) { // code here } |
It's pretty easy to see how the dialect file is structured, just keep adding commands to it until you're done.
A big benefit of putting the commands for a dialect in it's own file is that other AI devs can easily see what commands you support and how you implemented them.
Triggering dialect commands
Now we need to hook up our events...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
include("path/to/APIs/Babel.js"); chat.dialects = {}; include("path/to/chat.dialects.nb.js"); // NullBot dialect function eventChat(sender, to, message) { if (chat.isBabel(message)) { eventBabel.apply(this, chat.fromBabel(message)); } else { // normal chat msg handling goes here } } function eventBabel(sender, to, message, dialect, data) { var reply; if (dialect in chat.dialects && message in chat.dialects[dialect]) { // run the command reply = dialects[dialect][message].call(this, sender, message, data); // send a reply? if (reply) chat(sender, chat.toBabel.apply(this, reply)); } } |
Re-using commands for human interactions
What happens if we want to make it easy for humans to use the "who" command, and any of the other commands? Well, we could do stuff like this...
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
const AI = true; const HUMAN = false; // update eventChat... function eventChat(sender, to, message) { if (chat.isBabel(message)) { eventBabel.apply(this,message = chat.fromBabel(message); message.push(AI); }// elsereply {mode eventHumaneventBabel.callapply(this, sender, message); } }else { const message AI = true; const HUMAN = false;message.split("!").pop().toLowerCase(); eventBabel.call(this, sender, message, "nb", null, HUMAN); } } function eventBabel(sender, to, message, dialect, data, replyMode) { var reply; if (dialect in chat.dialects && message in chat.dialects[dialect]) { // run the command reply = dialects[dialect][message].call(this, sender, message, data, AIreplyMode); // send a reply? if (reply) { reutrn (replyMode == AI) ? chat(sender, chat.toBabel.apply(this, reply)); }// } function eventHuman(sender, message) { message = message.split("!").pop().toLowerCase(); var reply; if (dialect in chat.dialects && message in chat.dialects[dialect]) { // run the command (note: no data) reply = dialects[dialect][message].call(this, sender, message, null, HUMAN); // send a reply? if (reply) chat(sender, reply); ai readable : chat(sender, reply) // human readable } } } |
And then update the replies sent by commands in the dialect file...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// create an object for the NullBot commands we are implementing... chat.dialects["nb"] = {}; // now let's implement NullBot's "who" command: chat.dilalects.nb["who"] = function(sender, message, data, aiFormatreplyMode) { // code here // send different reply if sender is human return (aiFormatreplyMode == AI) ? ["iAm", "nb", {personality: "mc"}] // forai AIsreadable : "I'm researching Machineguns and Cannons"; // forhuman Humansreadable } // etc... |
Div | ||||||
---|---|---|---|---|---|---|
| ||||||
Availability
Requires:
|
Div | ||||
---|---|---|---|---|
| ||||
ContentsJump to:
|
Div | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
Babel APITopics:
|