alaingilbert / Turntable-API

Allows you to create bots for turntable.fm
http://alaingilbert.github.com/Turntable-API/
MIT License
317 stars 97 forks source link

Help With Splitting #216

Open mnafricano opened 11 years ago

mnafricano commented 11 years ago

I have some code and it says:

if (data.text.split(" ")[0] == "mail") {
    var mailto = data.text.split(" ")[1];
    var message = data.text.split(" ")[2];
    bot.getUserId(mailto, function (data) {
        mailto = data.userid;
        bot.pm(message, mailto);
    });
}

Now, when I run this, it only sends the first word of my message, how can I make this say everything that is equal to and greater than 2? Thanks!

samuri51 commented 11 years ago

not gonna lie ive never used string split like that... its not readable at all. but yeah what your doing with split is splitting all the words in the data.text string into an array of strings based on an empty space as a delimator. and in this you specified message[2] which only returns the string found at index 2, aka just one item. if you want more than that you have to concatenate the strings. haven't tested this but it should work.

var mailArray = data.text.split(" ");

if(mailArray[0] == 'mail')
{
    var mailto = mailArray[1];
    var message;

    for(var i = 2; i < mailArray.length; i++)
    {
        message = mailArray[i] + ' ';
    }

    bot.getUserId(mailto, function(data2)
    {
        bot.pm(message, data2.userid);
    });
}

also take note of this... i'm assuming that mailArray[1] is someones name. this logic wont work if someones name is more than one word separated by spaces... it'll only work with single word names

gizmotronic commented 11 years ago

I recommend CodeAcademy if you're looking to better your JavaScript skills. Also, W3Schools is a good basic JavaScript reference site.

Using the RegExp object makes this a lot simpler, but regular expressions are harder to understand if you've never seen them. It's worth the effort.

matches = data.text.match(/^mail\s+(\S+)\s(.+)/);
if (matches && matches[0]) {
  bot.getUserId(matches[0], function (user) {
      bot.pm(matches[1], user.userid);
  });
}

(Beware: you'll have trouble with this simple solution if the user's name has a space in it.)

If you're really stuck on something but it's not an actual issue with Turntable-API, and you're not finding answers here, on W3Schools, or Stack Overflow, may I suggest asking on Stack Overflow first? That's a much more appropriate forum than here in the issue tracker. :)

technobly commented 11 years ago

I like to use keywords for separating stuff like this. Looks like you are coding up a message bot. Heh

My message bot uses a command sequence like this:

/give User name message Hey how is it going?

Pretty easy to parse that then with keywords "give" and "message"

If you add the @ symbol in there and make it automatically figure out if it doesn't work without @ to try again with @ for twitter usernames. That will make it more challenging, but also more reliable.