javascript - Is there is a way to check a word is same to another word?
问题描述
I am making bot that check if there is a user who said bad words. For some reason, it didn't do anything
var badWord = "not"
let splittedMessage = message.content.split(" ")
for (let i=0; i < splittedMessage.length; i++) {
for (let u=0; u < badWords.length; u++) {
if (splittedMessage[i] === badWords[u]) {
badWord = "yes"
}
}
}
if (badWord === "yes") {
console.log("Deleting message")
message.delete()
message.channel.send(`${message.author} has been warned. Reason: Bad word usage`)
message.author.send(`You has been warned in DoGame. Reason: Bad word usage`)
}
Bad words array
const badWords = ["fuck","hell","shit","bitch","asshole","bastard"]
解决方案
An Idiot's Guide Has a great example of how to detect swear words in the message without the need of loops.
const swearWords = ["darn", "shucks", "frak", "shite"];
if (swearWords.some(word => message.content.toLowerCase().includes(word))) {
message.reply("Oh no you said a bad word!!!");
}
推荐阅读
- react-admin - 无法重置 SelectInput 中的所有选项
- node.js - makeExecutableSchema 上的语法错误:意外的名称“用户”(GraphQL)
- algorithm - 获取列表列表的类似笛卡尔积,长度不等,并且对每个项目应用函数('a -> 'b 列表)
- php - 使用 VSCode 和 Docker 调试 PHP
- google-cloud-platform - 日志输出int的值有错误
- entity-framework - 如何在实体框架 LINQ 中格式化数字(不带尾随零)?
- php - PHP 7.2 - 如何在字符串中有函数体时动态创建匿名函数
- php - PHP Rand 函数似乎是加权的
- python - 绘制具有不同颜色的多个值的字典
- math - 缩小一个圆,同时保持所有点在里面