Skip to content

basic add remove example

chris larosa edited this page Jul 18, 2013 · 12 revisions

fully functional basic bot that adds/removes names

example usage:

/addname @nacheese
/removename @nacheese

var Bot = require('ttapi');
var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'; //set the auth of your bot here.
var USERID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'; //set the userid of your bot here.
var ROOMID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'; //set the roomid of the room you want the bot to go to here.

var bot = new Bot(AUTH, USERID, ROOMID); //this initiates the bot
bot.listen(8000, '127.0.0.1'); //pings localhost, not needed to run on your computer, but needed to host on a server


global.names = []; //global declared array


bot.on('speak', function(data)
{
    if(data.text.match(/^\/addname/))
    {
        var name = data.text.slice(9).trim(); //name given
        var nameInList = names.indexOf(name); //is name in list?
        
        console.log('name to add: ' + name); //so u can see what the name is (optional)
        
        if(nameInList == -1) //if name is not in list (since -1 can't be a valid index)
        {
            names.push(name); //add name given to the end of the list        
        }        
        
        bot.speak(names); //makes bot say the list out loud
    }
    else if(data.text.match(/^\/removename/))
    {
        var name = data.text.slice(12).trim(); //name given, longer word so longer cutoff
        var nameInList = names.indexOf(name); //is name in list?
        
        console.log('name to remove: ' + name); //so u can see what the name is (optional)
        
        if(nameinList != -1) //if name was found in list
        {
            names.splice(nameinList, 1); //remove one item from name array starting from the index
        }                                //where name was found

        bot.speak(names); //makes bot say the list out loud
    }
});
Clone this wiki locally