Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - 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 a n****" 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); 
    }

});
question from:https://stackoverflow.com/questions/65645260/flagging-words-in-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)}'`);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...