Nick
Nick

Reputation: 105

Message filter ignoring allowed words

I've been trying to redo a message filter for a little while now, and I'm stumped on something with it. I have a list of different words in different categories that the filter blocks, but there's also a place for allowed words, which it'll ignore. Here's my code for it.

    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;
     }

Anytime someone says the word night, for instance, it counts it as racism because it checks for the first 3 letters and deems it racist, even though the word night is in the allowed words list, not sure what I did wrong. I'm trying to make it so it ignores the word night (or any other words in the list,) but continue to check the message.

Upvotes: 0

Views: 127

Answers (1)

Tarazed
Tarazed

Reputation: 2665

I'm having a bit of trouble following your logic in this, but I would consider inverting it. Split the message into an array of words and then check each word in the message to see if it's in the blacklist and not in the whitelist.

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.
}

Upvotes: 1

Related Questions