0

我正在尝试制作我的不和谐机器人,以便每当用户输入某些单词时它都会在控制台中进行标记。如果第一个单词包含设置为标记的单词,则代码完全可以正常工作,但是如果有人输入“You are an****”,它不会检测到,但只有“n****”会标记,因为它当说出多个应该标记的单词时,标记也有问题。

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

});
4

1 回答 1

0

用户发送消息时,使用正则表达式查找所有匹配项。

尝试这样的事情:

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

于 2021-01-09T17:25:41.963 回答