skip to Main Content

Is there any way that I can stop my Discord bot from deleting all messages? I only want to delete messages that include banned words.

client.on('messageCreate', async message => {
  if (message.author.bot || !message.guildId) 
    return;

  if (message.content.includes(badWords))
    await message.reply("Not cool buddy you getting a timeout")
  message.delete()
  return;
})

Can anyone solve this because I couldn’t find anything? I asked some people too but I couldn’t understand what they mean.

3

Answers


  1. It looks like a typo. You don’t have curly braces around the whole block you want to execute when that if statement is true:

    client.on('messageCreate', async message => {
      if (message.author.bot || !message.guildId) 
        return;
    
      if (message.content.includes(badWords)) {
        await message.reply("Not cool buddy you getting a timeout")
        message.delete()
        return;
      }
    })
    
    Login or Signup to reply.
  2. I don’t know anything about Discord but this part of your logic doesn’t look right…

    if (message.content.includes(badWords))
    

    Because "badWords" must be an array of strings and you’re meant to loop through them.

    Probably something like this…

    for(var i=0; i<badWords.length; i++){
     
    
     if (message.content.includes(badWords[i])){  // <-- changed here
    
         await message.reply("Not cool buddy you getting a timeout");
    
         message.delete();
    
         break;  // <-- changed here
    
      }
    
    }
    
    Login or Signup to reply.
  3. Yes bro its a typo, You don’t have curly braces around the whole block you want to execute when that if statement is true.

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