邮件过滤器忽略允许的单词

时间:2020-02-27 18:29:58

标签: javascript discord.js

我一直在尝试重做消息过滤器一段时间,而我对此感到困惑。我有过滤器阻止的不同类别的不同单词的列表,但是还有一个允许的单词的位置,它将被忽略。这是我的代码。

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

例如,有人说“夜晚”一词时,它会把它视为种族主义,因为它会检查前三个字母并认为它是种族主义,即使“夜晚”一词在允许的单词列表中,也不确定我做错了什么。我正在尝试使它忽略单词Night(或列表中的其他任何单词),但继续检查该消息。

1 个答案:

答案 0 :(得分:1)

在遵循您的逻辑时,我遇到了一些麻烦,但我会考虑将其反转。将邮件拆分为单词数组,然后检查邮件中的每个单词,以查看其是否在黑名单中,而不是白名单中。

// test case
[Theory]
public void GET_All_ReturnSuccessAndCorrectContent()
{
    try
    {
        // Arrange
        var controllerUnderTest = CreateApiControllerIT();

        // Act
        var response = controllerUnderTest.GetAll();
        response = controllerUnderTest.GetAll();

    } 
 ...
}