为我的javascript数组提供对includes()的访问权限
在此之前,我一直在为我的discord.js bot制作一个单词过滤程序,所以请原谅我说的不好的话强> 由于无法在includes()中添加额外的参数,我决定创建一行变量:为我的javascript数组提供对includes()的访问权限,javascript,arrays,node.js,discord,Javascript,Arrays,Node.js,Discord,在此之前,我一直在为我的discord.js bot制作一个单词过滤程序,所以请原谅我说的不好的话 由于无法在includes()中添加额外的参数,我决定创建一行变量: var filteredwords = ['asshole', 'fuck'] 但现在我想在下面的代码行中添加这些单词(将添加更多): if (message.content.includes('asshole')); 所以我不想用“混蛋”来放置阵列?我该怎么做?因为我是JS的初学者,所以我无法理解有类似问题的其他主题。如果
var filteredwords = ['asshole', 'fuck']
但现在我想在下面的代码行中添加这些单词(将添加更多):
if (message.content.includes('asshole'));
所以我不想用“混蛋”来放置阵列?我该怎么做?因为我是JS的初学者,所以我无法理解有类似问题的其他主题。如果你用noob语言解释就可以了。:)
如果有用,这是我的完整代码:
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}`));
}
});
如果使用Array elem as参数调用的给定函数之一为true,则Array.prototype.some迭代数组并返回true,因此,如果它至少包含一个坏字…您可以在此处使用正则表达式匹配。更快的实现
var filteredwords = ['bad-word', 'another-bad-word']
var message = 'Hello this string has a bad-word in it'; // Use your messsage.content here
// In a regex - '\b' start or end of a word
// " .join('\\b|\\b') " - gives me a regex string with each of the values from filteredwords array concatinated to for a 'OR' expression
// Here is th regex exp taht is generated `\basshole\b|\bfuck\b`
if( (new RegExp( '\\b' + filteredwords.join('\\b|\\b') + '\\b') ).test(message) ){
alert('match'); // Here the word were matched
}else{
alert('no match'); // Wooho, a safe message
}
专业提示:
RegEx解决方案的突出之处在于,您可以进行不区分大小写的匹配,并且对于作为另一个单词的一部分出现的坏单词,例如“dambad word”将提供坏单词的匹配
编辑:更新const Discord=require('Discord.js')发布的完整代码的答案;
const client=new Discord.client();
var filteredwords=[“混蛋”,“操”]
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 ( (new RegExp('\\b' + filteredwords.join('\\b|\\b') + '\\b')).test(message.content ) ) {
message.reply('Do not swear please');
message.delete();
var colors = require('colors/safe');
console.log(colors.red(`The following message got deleted:${message.content}`));
}
});
如果您想在句子hello there youcurseword
中检测到saycurseWord
等非整词进行匹配(也不区分大小写),您可以将最后一个If
条件替换为:
// We can get rid of '\b' to make the search not limited to whole-word matching
if ((new RegExp(filteredwords.join('|'))).test(message.content))
// We can use 'i' flag to make the matching case insensitive
if ((new RegExp(filteredwords.join('|') , 'i')).test(message.content))
由于使用了“includes”方法,我认为“message.content”的类型是数组
因此,问题可以看作是比较两个阵列。您可以简单地应用两个循环进行检查,也可以使用reduce方法进行检查
var messageA = {
content: "Here is a asshole in it.".split(" "),
};
var messageB = {
content: "Here is a hole in it.".split(" "),
};
var filteredwords = ['asshole', 'fuck'];
var isValidA = messageA.content.reduce(function(pre, cur) {
if (pre) return !!filteredwords.indexOf(cur);
return pre;
}, true);
var isValidB = messageB.content.reduce(function(pre, cur) {
if (pre) return !!filteredwords.indexOf(cur);
return pre;
}, true);
console.log(isValidA); // false
console.log(isValidB); // true
为什么要发出警报?我已经有了msg.reply命令行。无论如何,我会试试你的代码:)@M.Kaçiran我相信这个警告是用来说明一点,而不是实际建议你应该如何实现你的代码。为了确保我添加了整个代码,它不仅仅是删除。我对您发布的这些代码非常不熟悉。下面是一个JSFIDLE,其中包含了我在上面发布的代码片段,一切正常:当我使用bot令牌运行此代码时,它不起作用:您必须在这里更加具体。粘贴到错误堆栈中,完全没有错误。它就是不起作用。控制台中没有错误。
var messageA = {
content: "Here is a asshole in it.".split(" "),
};
var messageB = {
content: "Here is a hole in it.".split(" "),
};
var filteredwords = ['asshole', 'fuck'];
var isValidA = messageA.content.reduce(function(pre, cur) {
if (pre) return !!filteredwords.indexOf(cur);
return pre;
}, true);
var isValidB = messageB.content.reduce(function(pre, cur) {
if (pre) return !!filteredwords.indexOf(cur);
return pre;
}, true);
console.log(isValidA); // false
console.log(isValidB); // true