如何检查邮件内容是否包含数组中的任何项目? [英] How do I check if message content includes any items in an array?

查看:74
本文介绍了如何检查邮件内容是否包含数组中的任何项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制造一个不和谐的机器人,我试图使用数组来创建一个禁止的单词列表.我似乎找不到如何使这项工作.这是我当前的代码:

I'm making a discord bot and I'm trying to make a forbidden words list using arrays. I can't seem to find out how to make this work. Here's my current code:

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中的任何内容并将其删除.我该怎么办?

If you can't tell, I'm trying to check if the user's message has anything that's in the array forbidenWords and remove it. How do I do this?

推荐答案

您发布的代码将检查整个消息的内容是否为数组的成员.要完成所需的操作,请遍历数组并检查消息是否包含每个项目:

The code you posted checks if the entire message's content is a member of your array. To accomplish what you want, loop over the array and check if the message contains each item:

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")

(By the way, you misspelled "forbidden" in your variable name)

这篇关于如何检查邮件内容是否包含数组中的任何项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆