skip to Main Content

I would like my bot to delete a message by a user if it contains a certain word.
My bot work with node JS.
Thx for the help.

i tried to do a little code to delete the message, and it works but
the code delete the previous messages sent by the user
here is my code:

client.on("messageDelete", message => {
  console.log(message)
  if(message.content === "Bad word"){
    message.delete
  }
})

2

Answers


  1. Store badWords inside array and then iterate through it and delete it.

    const badWords = ["bad", "word", "example"];
    
    client.on("messageDelete", message => {
      const content = message.content.toLowerCase();
      
      for (let i = 0; i < badWords.length; i++) {
        if (content.includes(badWords[i])) {
          message.delete()
            .then(() => console.log(`Deleted "${message.content}" from ${message.author.username}`))
            .catch(console.error);
          break;
        }
      }
    });
    
    Login or Signup to reply.
  2. Just in case you haven’t noticed (not say you haven’t), but you forgot to add () at the ".delete()" this is crucial for it to work. Apart from that the answer should work. Your code should look like this:

    client.on("messageDelete", message => {
      console.log(message);
      if(message.content === "Bad word"){
        message.delete();
      }
    })
    

    and not

    client.on("messageDelete", message => {
      console.log(message)
      if(message.content === "Bad word"){
        message.delete
      }
    })
    

    Apart from this if you want you code to censor words that are lowercase and not just one word, but an array of words you can use the following code that I personally use myself.

    var LoweredString = message.content.toLowerCase();
    
      // Censor Words
    
      var RipWord = LoweredString
        .replace(" ", "")
        .replace("$", "s")
        .replace("!", "i")
    
      for (var I = 0; I < BadWords.length; I++) {
        var Word = BadWords[I][0];
        var Warns = BadWords[I][1];
    
        if (RipWord.includes(Word) && ['832075875612491786'].includes(message.guild.id)) {
          console.log(RipWord)
          message.delete().then(() => {
            message.reply(censor).then((msg) => {
              var User = msg.mentions.users.first();
    
              for (var In = 0; In < Warns; In++) {
                msg.channel.send("!warn <@" + User.id + ">");
              };
            });
          });
        };
      };
    

    For this to work you need to form an array of bad words to use, but you will figure it out. The code censors lower and upper string words, words with letters replaced with numbers and signs and also warns the offending user.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search