简体   繁体   中英

Flagging words in discord.js

I'm trying to make my discord bot so that it flags in the console whenever certain words are typed by a user. The code works completely fine if the first word contains the words that are set to flag, but if someone put "You are an****" it would not detect but just "n****" does flag beacuse of this it also has problems flagging when multiple words that should be flagged are said.

client.flags = new Discord.Collection();

const flagFiles = fs.readdirSync('./flag/').filter(file => file.endsWith('.js'));
for(const file  of flagFiles){
    const flag = require(`./flag/${file}`);

    client.flags.set(flag.name, flag);
}


client.on("message", function(message){
    console.log("Channel:" + color.blue(message.channel) + " " + "Author:" + color.blue(message.author) + " " + "Message:" + color.blue(message.content))
    if (message.author.bot) return;

    const flagwords = ["spam","Spam","Nig", "nig", "N19", "n19"];
    const args1 = message.content.slice(flagwords).split(/ +/);
    const msg = args1.shift().toLowerCase();
    
    if (msg.includes("spam") || msg == "spam" || msg == "spam" || msg.includes("spam")) {
        client.flags.get("spam").execute(message, args1); 
    }
    if (msg.includes("nig") || msg == "nig" || msg == "nig" || msg.includes("nig")) {
        client.flags.get("nigga").execute(message, args1); 
    }
    if (msg.includes("n19") || msg == "n19" || msg == "n19" || msg.includes("n19")) {
        client.flags.get("nigga").execute(message, args1); 
    }

});

Use regex to find all matches in every moment when user send message.

Try something like this:

 let message = "niggers, nigger spam 1234. niga N19 nig" const flagWords = [/spam/g, /Spam/g, /Nig/g, /nig/g, /N19/g, /n19/g]; flagWords.forEach(checkMessage); function checkMessage(item, index) { if (message.match(item).= null) console:log(`Message: '${message}' found. '${message;match(item)}'`); }

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