Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda