Discord.js delete messages not commands



Hello I am republishing this post because in the previous one I must have explained it wrong. I meant that after sending a MESSAGE (not command) for example f3eufgjnei gergergce4ger gergrg or other message to spam it will be deleted (not after a few minutes of spam) but immediately delete after sending please help, if you do not know the answer, please do not send comments marked documentation because there is nothing in the previous post many people sent me documentation that did NOT help, code below : )

if (message.content === `${prefix}verify`) {
    message.delete();
    if (message.channel.id === '838040640377585664') {
        let role = message.guild.roles.cache.find(r => r.id === "838037327589670962");
        message.member.roles.add(role);
        message.author.send(`test`)
    }
}

The $verify COMMAND deletes and that’s about it but other MESSAGES don’t delete (messages not commands!)

Answer

First check the channel id, then check if the message content is verify else do message.delete() which will delete the message if its not a command. You are doing it in the reverse order, cause you are checking if the message content is verify and then doing message.delete() which deletes the command.

Eg:

if (message.channel.id === '838040640377585664') {
    if (message.content === `${prefix}verify`) {
        return message.channel.send('verified'); //or do your thing with a return statement
    }
    message.delete();
}

Basically it will first check if its the channel , and check if the message is !verify if it is, then it will send what ever you do inside the if statement but remember to return(stops further execution of program). If its not the case, then it will delete. Or you can include them inside an if, else statement.

Eg:

if (message.channel.id === '838040640377585664') {
    if (message.content === `${prefix}verify`) {
        //do your thing
    }
    else{
        message.delete();
    }
}


Source: stackoverflow