如何检查消息内容是否包含数组中的任何项目?
How do I check if message content includes any items in an array?
我正在制作一个不和谐的机器人,我正在尝试使用数组制作一个禁用词列表。我似乎无法找到如何使这项工作。这是我当前的代码:
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
中的任何内容并将其删除。我该怎么做?
您发布的代码检查整个消息的内容是否是您数组的成员。为了完成你想要的,遍历数组并检查消息是否包含每个项目:
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;
}
}
(顺便说一下,你在变量名中拼错了 "forbidden")
您可以改用 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.`));
}
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
在 "modern" 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?
我正在制作一个不和谐的机器人,我正在尝试使用数组制作一个禁用词列表。我似乎无法找到如何使这项工作。这是我当前的代码:
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
中的任何内容并将其删除。我该怎么做?
您发布的代码检查整个消息的内容是否是您数组的成员。为了完成你想要的,遍历数组并检查消息是否包含每个项目:
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;
}
}
(顺便说一下,你在变量名中拼错了 "forbidden")
您可以改用 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.`));
}
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
在 "modern" 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?