我已经尝试重做消息过滤器有一段时间了,但我对它的一些东西感到困惑。我有一个过滤器阻止的不同类别中的不同单词的列表,但也有一个允许的单词的位置,它会忽略它。这是我的代码。
for (var i in curse) {
if(msg.includes(curse[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Profanity.** \n${ifcont}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation).then(msg => {msg.delete(30000)})
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
for (var i in inap) {
if(msg.includes(inap[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Inappropriate Content.** \n${ifcont}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation).then(msg => {msg.delete(30000)})
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
for (var i in encvio) {
if(msg.includes(encvio[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Encouraging Violence or Hatred towards others.** \n${ifcont}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation).then(msg => {msg.delete(30000)})
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
for (var i in web) {
if(msg.includes(web[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Website Links or Advertising.** \n${ifcont}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation).then(msg => {msg.delete(30000)})
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
for (var i in racism) {
if(msg.includes(racism[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Racism.** \n${ifcont}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation).then(msg => {msg.delete(30000)})
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
for (var i in blacklistwords) {
if(msg.includes(blacklistwords[i])) {
const filterViolation = new Discord.RichEmbed()
.setColor('#ff0000')
.setAuthor(message.author.username, message.author.avatarURL)
.setTitle('Filter Violation')
.setDescription(`${rule} **Blacklisted Words Usage.** \n${ifcont}`)
.setTimestamp()
.setFooter(copyright);
message.delete()
message.author.send(filterViolation).then(msg => {msg.delete(30000)})
message.channel.send(msgvio).then(msg => {msg.delete(15000)})
logger.write(logviolation)
}
}
for (var i in allowed) {
if(msg.includes(allowed[i])) return;
}
例如,每当有人说“夜晚”这个词时,它都会将其视为种族主义,因为它会检查前 3 个字母并认为它是种族主义,即使“夜晚”这个词在允许的单词列表中,也不知道我做错了什么。我正在尝试使其忽略“夜晚”一词(或列表中的任何其他单词),但继续检查消息。
最佳答案
我在遵循你的逻辑时遇到了一些麻烦,但我会考虑颠倒它。将消息拆分为单词数组,然后检查消息中的每个单词,看看它是否在黑名单中而不是在白名单中。
let checkMessage = msg.split(" ");
for (let i = 0; i < checkMessage.length; i++)
{
if(allowed.includes(checkMessage[i]) {
continue;
}
if(racism.includes(checkMessage[i])) {
// Violation!
}
if(blacklist.includes(checkMessage[i])) {
// Violation!
}
// and so on.
}
关于javascript - 消息过滤器忽略允许的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60439604/