0

I'm adding an auto-mod for swearing, I want the bot to look for any word from the list in config.json named "badwords" and delete it, which works, but if the member adds " "(space) or "_" or anything like that, it bypasses the check, so I added .replace(//s/g,'') which works for space, but for dash and other stuff, I wanted to use a list in config.json, but I can't seem to get the bot to run thru the list, there are no errors, so how can I fix this problem?

here is my code:

const config = require('../../config');


module.exports = async (client, message) => {
    if (!message.guild) return;

    if(!message.author.bot) {
    
    var badwords = config.badwords;
    var thingstoremove = config.thingstoremove;

    for (var i = 0; i < badwords.length; i++) {
      if (message.content.toLowerCase().replace(thingstoremove[8],'').includes(badwords[i])) {
        message.delete()
        message.reply("Watch your language!").then(m => m.delete({timeout: 10000}))
        break;
      }
    }
  }
}

config.json:

{
  "badwords": ["test1", "test2", "test3", "test4", "test5"],
  "thingstoremove": ["-", "_", ".", ",", "`", "~", "@", "#"]
}

Thanks.

Hamoodi youtube
  • 213
  • 1
  • 9
  • Can you share us the `config.json`? Such as that it at least includes the `badwords` & `thingstoremove` entries – LeWolfie Dec 12 '21 at 20:34
  • it includes bad words, will that get me flaged? – Hamoodi youtube Dec 12 '21 at 20:40
  • I'll change the bad words to test1, test2 ect... just incase. – Hamoodi youtube Dec 12 '21 at 20:41
  • Added the config file. – Hamoodi youtube Dec 12 '21 at 20:42
  • By the way, I wanted to ask, how can I stop is from filtering the words like gl*ass* as it deletes it even if it's not really the word – Hamoodi youtube Dec 12 '21 at 20:55
  • As you mention, your regex is *too* inclusive to detect those *innocent* words, and [you should beware of these issues](https://stackoverflow.com/a/24527/14651711) when it comes to bad word detection. Thus, currently I cannot give you an exact answer, besides detecting the exact words; however, there is also a good add-on to your code that you could implement, and that is intentional "misspellings" [(see)](https://stackoverflow.com/a/24615/14651711). For now, I will try to solve your issue on this question. – LeWolfie Dec 12 '21 at 22:51
  • Ok, I removed `.includes`. – Hamoodi youtube Dec 12 '21 at 22:59
  • Nvm, I won't add swearing protection, unless you can help, but it seems too complicated than it seemed before, I'm going to look into basic commands. – Hamoodi youtube Dec 12 '21 at 23:07
  • By the way, I have ran into several issues: I used the latest Discord.js version (v13) but it had issues, so it seems you are using v12; and the installation of that version had problems. Now everything is fine. Will start executing your code again. – LeWolfie Dec 12 '21 at 23:32
  • Also, never give up on your code because the idea is complex to implement — with help, issues can be easier to solve; I have solved the issue you mentioned on this (which I already knew a while ago, but wanted to use the code on a Discord bot to see if everything was working). Though, swearing protection is itself difficult to implement, **if you want it to be the most accurate possible**; I suggest you to search on the internet about it, or ask the questions you need on Stack Overflow if you wish. – LeWolfie Dec 13 '21 at 00:17

2 Answers2

1

Use this simple one-liner to get the fully replaced string

let newStr = thingstoremove.reduce((a, c) => a.replaceAll(c, ""), message.content)

And then a simple check with this:

if (badwords.some(b => newStr.includes(b))) {
  message.delete()
  message.reply("Watch your language!").then(m => m.delete({ timeout: 10000 }))
}
MrMythical
  • 8,908
  • 2
  • 17
  • 45
0

The issue is:

  • The code is attempting to replace an undefined value from the message, as index no. 8 is not found on the array (since the array has 8 items; and indexes start from 0 (see), so the last index would be 7), i.e. thingstoremove[8]
  • And also, if you are trying to remove all the characters from your array, you need to include them in a regex — if you are going to use .replace — instead of a single element.

Therefore, you should create a set of characters from the array on the regex, to capture any of the characters, and replace them:

const regex = new RegExp(`[${thingstoremove.join('')}]`, 'g')

And then use the regex on .replace:

if (message.content.toLowerCase().replace(regex, '').includes(badwords[i]))

Resulting code:

const config = require('../../config');

module.exports = async (client, message) => {
    if (!message.guild) return;

    if (!message.author.bot) {
        var badwords = config.badwords;
        var thingstoremove = config.thingstoremove;
    
        const regex = new RegExp(`[${thingstoremove.join('')}]`, 'g')
    
        console.log(regex)

        for (var i = 0; i < badwords.length; i++) {
            if (message.content.toLowerCase().replace(regex, '').includes(badwords[i])) {
                message.delete()
                message.reply("Watch your language!").then(m => m.delete({timeout: 10000}))
                break;
            }
        }
    }
}
LeWolfie
  • 82
  • 7