Search code examples
javascriptregextwitch

if statement with RegExp alway triggering


I'm working on a twitch bot and I'm currently trying to get the bot to welcome people to the stream, problem is, one of the emote is "HeyGuys" and typing this emote alone would trigger him as there is both "hey" and "guys", so I wanted to use regexp to look at the entire word to fix that, but now, it triggers everytime!

here's the code:

var welcomed;
function checkWelcomeMsg(channel, msg, usr) {
    welcomed = false;
    for(var i = 0; i < welcome.length; i++) {
        if(new RegExp('\\b' + welcome[i] + '\\b') && !welcomed) {
            for(var i = 0; i < chatNames.length; i++) {
                if(new RegExp('\\b' + chatNames[i] + '\\b')) {
                    console.log(getRandomResponse(greetings)+usr.username+"!");
                    welcomed = true;
                    console.log(welcomed);
                    break;
                }
            }
        }
    }
}

and here are the arrays that the code is looking at:

//Welcome; Thanks; Goodbye syntaxes
var welcome = ["hi", "hey", "hello", "o/", "HeyGuys", ];
var chatNames = ["chat", "everybody", "people", "everyone", "guys"];

//Responses
var greetings = ["Hello ", "HeyGuys ", "Hey "];

the code looks at all the messages for one with both a word from "welcome" and "chatNames" and chooses a random answer from "greetings"

putting console.log after the if statements gave me "\bhi\b" and "\bchat\b" everytime I typed something in chat


Solution

  • I would not use Regexes as you can use Array.indexOf method

    Your code has a lot of issues, you should simplify the logic.

    You could do something like this

    var emotes = ["HeyGuys", ];
    
    function checkWelcomeMsg(channel, msg, usr){
        var welcomed = false;
    
        var usrMsg = msg.toLowerCase();
        // Split the msg 
        var words = usrMsg.split(" ");
    
        // check if the msg contains an emote
        for (var i = 0, len=words.length; i< len ; i++){
            if (emotes.indexOf(words[i]) > -1){
                welcomed = true;
                break;
            }
        }
    
        // If there is no emotes
        if (!welcomed){
            // check if the msg contains any of welcome words
            for ( i = 0, len=words.length; i < len ; i++){
                if (welcome.indexOf(words[i]) > -1){
                    welcomed = true;
                    break;
                }
            }
    
            // If not return
            if (!welcomed){
                return;
            }
    
            welcomed = false;
            // check if the msg contains any of the chatNames too
            for (i = 0; i < len ; i++){
                if (chatNames.indexOf(words[i]) > -1){
                    welcomed = true;
                    break;
                }
            }
        }
    
        if (welcomed){
            console.log(getRandomResponse(greetings)+usr.username+"!");
        }
    }

    As it's for a Twitch bot, and I'm not able to test this code, you should be careful using this code and test it.