简体   繁体   中英

Discord.JS Deleting a message if it contains a certain word

So I am trying too add moderation too my bot and since my server is fully SFW I am trying too make it so that my bot will delete a message if it contains an NSFW word, Here is my code. Im not sure if its correct but It is not working so I would imagine its not

message.content.includes === ("<Insert NSFW message or word here>")
message.delete(1)

Im still looking for help btw

You are doing great so far,

First I would you should create an array of NSFW words that you want to be censored

Ex:

const noNoWords = ["sex", "condum"];

After you have done that you should irritate through the array and see if the message's content includes the a NSFW word

Ex:

const noNoWords = ["sex", "condum"];

client.on("message", message => {
    var content = message.content;

    for (var i = 0; i < noNoWords.length; i++) {
        if (content.includes(noNoWords[i])){  
            message.delete();
            break
        }
    }
}

That works and all but if people type CAPS it wouldn't detect, or if people put in spaces, like thi s. In order to fix this we need to remove all special characters and make the string lower case.

Ex:

var stringToCheck = content;
stringToCheck.replace(/\s+/g, '').toLowerCase();

Then you plug it in and it should look like this:)

const noNoWords = ["sex", "condum"];

client.on("message", message => {
    var content = message.content;
    var stringToCheck = content.replace(/\s+/g, '').toLowerCase();

    for (var i = 0; i < noNoWords.length; i++) {
        if (content.includes(noNoWords[i])){  
            message.delete();
            break
        }
    }
}
if (message.content.includes("<Insert NSFW message or word here>")) message.delete(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM