2017-04-15 112 views
1
client.on('chat', function(channel, userstate, message, self){ 



    switch(message){ 
    case message.includes(emotes[0]): 
     numberOfEmotes[0]++; 
     console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)"); 
     break; 
    case message.includes(emotes[1]): 
     numberOfEmotes[1]++; 
     console.log("Emote1 has been used " + numberOfEmotes[1] + " time(s)"); 
     break; 
    case message.includes(emotes[2]): 
     numberOfEmotes[2]++; 
     console.log("Emote2 has been used " + numberOfEmotes[2] + " time(s)"); 
     break; 
    case message.includes(emotes[3]): 
     numberOfEmotes[3]++; 
     console.log("Emote3 has been used " + numberOfEmotes[3] + " time(s)"); 
     break; 
    } 

/* if(message.includes(emotes[0])){ 
    numberOfEmotes[0]++; 
    console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)"); 
    }*/ 


    //console.log("** " + message + " **"); 
}); 

当函数chat.on被称为消息的变量用字符串应该通过开关语句来运行,我有不同的字符串,并且如果阵列消息包含该数组中的一个字符串,运行该案例。但没有任何反应,这一切似乎正确,这可能是错误的?switch语句

+1

文档阅读'之开关更加紧密。 – 2017-04-15 07:38:13

回答

1

交换机无法按照您的预期工作。它将交换机中写入的值与案例块中的每个进行比较。所以在你的情况下,它会比较true/false与值的消息,它不会找到相同的值,因此不会发生任何事情。 你如果使用else语句或解析消息并排除像 消息“型/ emote1”的值 提取一切AFTE斜线,并把它在开关

+0

我基本上想检查字符串message,如果包含'emote [0]'的字符串'mesage',运行case语句。我使用switch语句,因为我必须这样做大约50次,目前我只有4个,但是我知道它可以工作后我将加入46个。 – Hooga