0

I'm trying to make it so that when someone types !familyfortunes %NAME% in message that it will run with the command and take the parameter (%NAME%) from the message and then store it in a variable.

I thought I could use if(message.toLowerCase.indexOf("!familyfortunes") !=-1){} and store $message into another variable but split to remove !familyfortunes so it only kept the username but that keeps returning the error:

greatbritishbg: !familyfortunes lol [10:53:39] error - TypeError: undefined is not a function at null. (example.js:68:29)

Which is the .split() part.

client.addListener('chat', function (channel, user, message) {
    console.log(user.username + ': ' + message);
    if (message.toLowerCase() === '!familyfortunes') { //If message contains !familyfortunes
        //var res = messageString.split("familyfortunes ");// Remove Family Fortunes to just get username
        client.say(channel, 'Latest Follower: ' + Follower.response);
        var raidName = Follower.response;
        io.emit('follow', raidName);
    }
}

Any advice on how I can tackle this would be appreciated. Thanks.

2 Answers 2

1

You are very close. Note that toLowerCase is a function, so you'll need to do message.toLowerCase().indexOf(). I dont know if the message must start with the command or not, but I assume that it has to. Quick example:

var msg = "!familyfortunes param1 param2";
if (msg.toLowerCase().indexOf("!familyfortunes") == 0) {
    var parts = msg.split(" ");
    if(parts.length > 1) {
        parts.shift();
        console.log(parts.join(" "));
    }
}

var parts = msg.split(" ") splits the string on space and return an array. msg.shift() removes the first entry in the array(!familyfortunes), and then msg.join(" ") take the remaining parts of the array and build a string, separated by a space.

If you only allow one value after the keyword, you can check for parts.lenght == 2 and just do console.log(parts[1]) to get the parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

hey @msfoster thanks for that, been looking at documentation and wondering for a while. With regards to the param1 and param2 I assume it uses the spaces to differentiate the different parameters? (Just trying to fully understand before I go ahead and use it)
1

You should try with a regexp

var r =/!familyfortunes\s+(.*)/i
var message = "!familyfortunes %NAME%"
message.match(r)
>> ["!familyfortunes %NAME%", "%NAME%"]

the regex match a string starting with !familyfortunes, one or ore spaces and then the argument of the command. The first group is the argument of the command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.