Javascript 如何让多个单词都触发同一件事

Javascript 如何让多个单词都触发同一件事,javascript,discord.js,Javascript,Discord.js,我想要的是这三个词,can,is,was,来触发相同的代码位。如果文本中有can、is或was,我不希望第一位运行,而是希望第二位运行 bot.on ('message', function (message){ const words = message.content.split(' '); if(words.includes('sans')) { const words = message.content.split(' '); if(!message.content.i

我想要的是这三个词,can,is,was,来触发相同的代码位。如果文本中有can、is或was,我不希望第一位运行,而是希望第二位运行

bot.on ('message', function (message){
const words = message.content.split(' ');

if(words.includes('sans'))
{
    const words = message.content.split(' ');
    if(!message.content.includes ('can', 'is', 'was'))
    {
        if(message.author.bot) return;
        var chance = Math.floor(Math.random() * 2); 
        if(chance == 0)
        {
            message.channel.send('<:annoying_sans:520355361425981440>');    
        }
        if(chance == 1)
        {
            message.channel.send('<:sans:519723756403425294>');    
        }
    }
    else
    {
        var chance = Math.floor(Math.random() * 3); 
if(chance == 0)
{
    message.channel.send('Maybe.');    
}
if(chance == 1)
{
    message.channel.send('Yes.');    
}
if(chance == 2)
{
    message.channel.send('No.');    
}

}

}
});
bot.on('message',函数(message){
const words=message.content.split(“”);
if(单词.includes('sans'))
{
const words=message.content.split(“”);
如果(!message.content.includes('can','is','was'))
{
if(message.author.bot)返回;
var chance=Math.floor(Math.random()*2);
如果(机会==0)
{
message.channel.send(“”);
}
如果(机会==1)
{
message.channel.send(“”);
}
}
其他的
{
var chance=Math.floor(Math.random()*3);
如果(机会==0)
{
message.channel.send('Maybe');
}
如果(机会==1)
{
message.channel.send('Yes');
}
如果(机会==2)
{
message.channel.send('No.);
}
}
}
});

我会这样做:

function includesQuestionWords(content, questionWords = ['can', 'is', 'was',]) {
    return questionWords.some(word => content.includes(word));
}

Array.prototype。有些
会对数组中的每个元素执行一个函数,如果其中一个元素为true,则返回true。

首先,您要创建一个名为“words”的数组,您永远不会使用它

此外,您必须迭代数组中的每个项,以检查数组中的每个字符串

另外,如果消息包含这三个单词中的任何一个,您没有向我们提供应该触发的代码,但是如果不包含它们,将触发什么

此外,您将使用相同的名称创建一个常量变量两次

bot.on ('message', function (message){
const words = message.content.split(' ');
if(words.includes('sans')){
    var questionwords = ['can', 'is', 'was',];
    for (i in questionwords) {
        if(words.includes(questionwords[i])) {
            if(message.author.bot) return;
            message.channel.send('<:download:519723756403425294>');
            return;
        }
    }
}
bot.on('message',函数(message){
const words=message.content.split(“”);
if(单词.includes('sans')){
var questionwords=['can','is','was',];
因为(我用疑问的话){
if(词语包括(问题词语[i])){
if(message.author.bot)返回;
message.channel.send(“”);
返回;
}
}
}

将此作为指导原则,不要只是复制粘贴它

我不确定值是多少,但我已经编写了一些代码,我认为您正在尝试实现这些代码。它将向您展示如何搜索can、is或was。我的两种解决方案都使用for in循环。您使用for in循环来循环对象

var sentence = 'can I go for a walk?';

checkIfQuestion(sentence);

function checkIfQuestion(str){
    var newWords = str.split(" ");
    var questionWords = ['can', 'is', 'was'];
    for(var char in newWords){ // Look at all the keys in the newWords object
        for(let i = 0; i < questionWords.length; i++){
            if(newWords[char] === questionWords[i]){
                console.log("It's a question!!");
                // Add your event here
            }
        }
    }
}
希望这些代码片段中的一个能够帮助您完成您的功能:)

var sentence = 'can I go for a walk?';

checkIfQuestion(sentence);
function checkIfQuestion(str){
    var newWords = str.split(" ");
    var questionWords = ['can', 'is', 'was'];
    for(var char in newWords){ // Look at all the keys in the newWords object
        if(questionWords.includes(newWords[char])){
            console.log('it is a question');
            // Add your event here
        }
    }
}