Search code examples
node.jstwitch

tmi.js message with input/argument?


Im am working on a twitch-bot using the tmi.js-npm and got a question about that.

I want to make a command like - "!giveaway" with an input, which could be anything. eg. "!giveaway pg". Then it should return "!pg". So the keyword "!giveaway" should be fixed, but the part after the blank, could be anything that is typed in.

My script looks like this:

client.on("chat", function (channel, user, message, self) {
 if (message === "!Giveaway" + " " + "input"){
        if(user["display-name"] === "username"){
            client.say("channel", "!" + "input");
        } else {
            client.say("channel", "No permissions");
        }
 };
});

Thanks :)


Solution

  • Something like this is most commonly used, adding more checks is advised, but depends on your needs.

    Checking the incoming message to see if it starts with a specific command, like so:

    message.startsWith("!giveaway")
    

    and with other logic

    if (message.startsWith("!giveaway")) {
        var input = message.split(' ')[1];
        if (input.count < 2) return;
        if (user["display-name"] === "username") {
            client.say("channel", "!" + input);
    
        } else {
            client.say("channel", "No permissions");
        }
    }