Search code examples
node.jstwitch

Node.js - How to parse arguments passed via an IRC chat message


EDIT: I am using the twitch-irc library from Github for Node.js, installed via npm.

This is such a basic question and I feel like a bit of an idiot for asking, but...

if (message.toLowerCase().indexOf('!os') === 0) {

}

Let's say I used this code, how would I add if statements for the arguments that the user provides? By this, I mean after typing !os, they would create a space and type something else, this would either be the 0th or 1st argument.

Would it be possible to just use this?:

if (message.toLowerCase().indexOf('kernel') === 4) {

}

In pseudo code, I want to do:

if (firstargument === 'kernel') {
    //Do stuff.
}

Thank you for your time.


Solution

  • You could simply use var args = message.match(/\S+/g) to get all the arguements (including '!os') in an array and use them as you wish.

    e.g.

    var args = message.match(/\S+g/);
    var cmd = args[1]; //do a length check before accessing args[1] though
    switch (cmd) {
        case 'kernel':
        // do stuff
        break;
        case 'process':
        // do stuff
        break;
        default:
        // invalid command
    }