在这之前,我一直在为我的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/