我制作了一个不和谐的机器人,我试图使用数组创建一个禁止的单词列表。我似乎无法找到如何使这项工作。这是我目前的代码:
if (forbidenWords.includes(message.content)) {
message.delete();
console.log(colors.red(`Removed ${message.author.username}'s Message as it had a forbidden word in it.`));
}
如果您无法说明,我试图检查用户的消息是否包含数组forbidenWords
中的任何内容并将其删除。我该怎么做?
答案 0 :(得分:4)
现代" JS:
forbiddenWords.some(word => message.content.includes(word))
以评论的逐行格式:
forbiddenWords // In the list of forbidden words,
.some( // are there some
word => // words where the
message.content // message content
.includes( // includes
word)) // the word?
答案 1 :(得分:1)
您发布的代码会检查整个邮件的内容是否是您阵列的成员。要完成您想要的操作,请遍历数组并检查消息是否包含每个项目:
for (var i = 0; i < forbidenWords.length; i++) {
if (message.content.includes(forbidenWords[i])) {
// message.content contains a forbidden word;
// delete message, log, etc.
break;
}
}
(顺便说一下,你的变量名中拼错了“forbi dd en”)
答案 2 :(得分:0)
您可以改为使用indexOf()方法:
if (forbidenWords.indexOf(message.content) != -1){
message.delete();
console.log(colors.red(`Removed ${message.author.username}'s Message as it had a forbidden word in it.`));
}
答案 3 :(得分:0)
Array.prototype.S = String.fromCharCode(2);
Array.prototype.in_array = function(e){
var r=new RegExp(this.S+e+this.S);
return (r.test(this.S+this.join(this.S)+this.S));
};
var arr = [ "xml", "html", "css", "js" ];
arr.in_array("js");
//如果 存在返回true , 不存在返回false