javascript - 让我的 javascript 数组能够访问includes()

标签 javascript arrays node.js discord

在这之前,我一直在为我的discord.js 机器人制作一个单词过滤程序,所以请原谅我说得不好!

由于你不能在includes()中添加额外的参数,我决定创建一个var行:

var filteredwords = ['asshole', 'fuck']

但现在我想将这些单词(还会添加更多单词)放在以下代码行中:

if (message.content.includes('asshole'));

所以我想放置数组而不是“ SCSS ”?我怎样才能做到这一点?由于我是 JS 的初学者,我无法理解具有类似问题的其他主题。如果用菜鸟语言解释一下就好了。 :)

如果有用,这是我的完整代码:

const Discord = require('discord.js');
const client = new Discord.Client();
var filteredwords = ['asshole', 'fuck']

function commandIs(str, msg) {
    return msg.content.toLowerCase().startsWith('--' + str);
}

client.on('ready', () => {
    console.log('The bot is started succesfully')
});

client.on('message', message => {
    if (commandIs('trump', message)) {
        message.reply('He is the president of the United States of America!');
    }
    if (commandIs('putin', message)) {
        message.reply('He is the president of Russia!');
    }
    if (commandIs('spacetaco', message)) {
        message.reply('He is the first user who joined Arcanews!');
    }
    if (message.content.includes('asshole')); {
        message.reply('Do not swear please');
        message.delete();
        var colors = require('colors/safe');
        console.log(colors.red(`The following message got deleted: 
${message.content}`));
    }
    });

最佳答案

if(filterwords.some(badword=>message.content.includes(badword))){
  alert("BAD!");
}

Array.prototype.some 迭代数组,如果使用数组 elem 作为参数调用的给定函数之一为 true,则返回 true,因此如果它至少包含一个坏词...

关于javascript - 让我的 javascript 数组能够访问includes(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43429420/

相关文章:

javascript - 如何使用nodejs脚本读取文件并更新特定行的文件

javascript - 如何在 JavaScript 中使用 "this"

javascript - 有没有办法在联合类型上调用数组原型(prototype)方法?

JavaScript - 将数组索引分配给返回值不起作用

c# - 在(2D)数组的END上开始数组搜索

javascript - Moment js diff函数在 native react 中不起作用

javascript - Mongoose 递归查找数据库中的元素

Javascript:在平面文件上存储/检索数据的最有效方法

javascript - Javascript 事件处理程序未按预期触发

javascript - jQuery 动画滞后