Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
How to replace letters from a list in `config.json` to noting using discord.js?

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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 }))
}
about 4 years ago · Juan Pablo Isaza Report

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;
            }
        }
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!