-2

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

Blü
  • 631
  • 2
  • 11
  • 26

1 Answers1

1

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.

Hacketo
  • 4,978
  • 4
  • 19
  • 34
  • this works but HeyGuys doesn't trigger it, he find hey and stops there, I think I'd have to use .toLowerCase() somewhere – Blü Jul 28 '15 at 14:35
  • @Mranth0ny62 Isn't your OP states that 'HeyGuys' should not trigger it ? – Hacketo Jul 28 '15 at 14:37
  • This code actually split the sentence from the user into a list of words so 'heyguys' can't trigger 'hey' and 'guys'. You earned a point about `toLowerCase` I edit my code – Hacketo Jul 28 '15 at 14:44
  • "HeyGuys" is in the array welcome, so if somebody types "HeyGuys chat" it should be triggered – Blü Jul 28 '15 at 14:55
  • @Mranth0ny62 Well, I did not see it in this array.. As any 'welcome' word has to be with a 'chatNames' word, "HeyGuys" should be an exception and should be contained in an other array that would be checked first. I updated the code – Hacketo Jul 28 '15 at 15:00
  • so I've just place HeyGuys in welcomeEmotes and I have copied the block that checked the welcome array and changed it for welcomeEmotes, I'm guessing I have to do it so that the welcome array doesn't get checked if one of the emote in welcomeEmotes has been found? – Blü Jul 28 '15 at 15:09
  • I updated the code, it may looks like what you've done. Yes – Hacketo Jul 28 '15 at 15:12
  • yup, I got everything working, I knew my code could've been made much simpler, thanks for your help! – Blü Jul 28 '15 at 15:39