您如何在“不和谐”消息中找到单词?

时间:2020-03-03 20:22:28

标签: javascript discord discord.js

因此,我已经找到了尽可能多的有关此主题的信息,但似乎没有什么是我想要的。现在,我正在学习编写自己的Discord机器人的代码。我碰到了一个要检测诸如亵渎过滤器之类的消息中的特定单词的地方。

我发现的唯一方法是使用“ message.content.includes()”,但这会发现单词中的模式,例如,如果我尝试删除“ rot”一词,它也会删除“ carrot”

我还知道排除“ .includes()”意味着即使该词在句子的后面被说出来,它也不会检测到它,因为那样的话它只会查找消息本身。

我的目标是拥有一个代码,如果我希望它删除“腐烂”,则仅当它在消息中的任何位置都是单独的单词时才应删除它。

谢谢。

编辑:我粘贴了当前代码:

const profanities = require('profanities/index.json');

for (let x = 0; x < profanities.length; x++) {
    if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
        message.channel.send('Oooooooh you said a bad word!');
        client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
        message.delete();
        return;
    }
}

3 个答案:

答案 0 :(得分:0)

bot.on("message", message => {
  let unallowed = ["foo", "bar"] //Words you want blocked
  let allowed = ["bypass", "words"] //Words that might appear in blocked words like glass in ass.
  let foundInText = false;
  let msg = message.content.toLowerCase()

  for (var i in unallowed) {
    if(msg.includes(unallowed[i])) foundInText = true;
 }


  for (var i in allowed) {
      if (msg.includes(allowed[i])) foundInText = false;
  }

  if (foundInText) {
      message.delete();
      message.channel.send('**Hey!** That word is not allowed here!').then(message => {
          message.delete(7500);
      })
    }
  })

编辑: 如果这就是您的意思,恐怕无法仅从邮件中删除某个单词。但是,如果您要删除包含一个单词的整个消息,那么我可以理解您在做什么。试试这个:

let checkMessage = message.content.toLowerCase(); // Puts the whole message to lowercase. If your profanities file is all caps change it to .toUpperCase(); but if it is all lowercase keep it the same.
for (let i = 0; i < checkMessage.length; i++) {
    let logchannel = message.guild.channels.find(x => x.name === `{name}`); // Name of the channel you want the logs to go to, this will help if you want to change the name and not grab the ID of the channel.
    const profanities = require('profanities/index.json'); // Make sure this is the correct file path of the profanities json.
    if(profanities.some(word => checkMessage.includes(word)) { // If the message includes any word from profanities.
        message.delete(); // Delete the message that was sent.
        message.channel.send(`Oooooooh you said a bad word!`).then(msg => {msg.delete(15000)}); // Send the channel a message saying that they said a bad word. Then delete the message after 15 seconds.
        client.channels.get(logchannel.id).send(`Message was deleted due to use of a blocked word:\n\n${message.content}`); //Send the log channel a message saying that there was a blocked word.
        return;
    }

答案 1 :(得分:0)

我建议将句子分成一个数组并查找特定的单词,然后在删除单词后再加入。

var sentence = "rabbit ate the carrot rot";

function deleteSpecific(word) {
  return sentence.split(" ").filter(each => each !== word).join(" ");
}

var res = deleteSpecific("rot");
console.log(res);

编辑:您可以与消息和亵渎文字相交,而不必像下面那样使用for Loop:

const profanities = require('profanities/index.json');

if ((message.content.split(" ").filter(Set.prototype.has, new Set(profanities))).length > 0) {
  message.channel.send('Oooooooh you said a bad word!');
  client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
  message.delete();
  return;
}

答案 2 :(得分:0)

"rabbit ate the carrot rot".replace(/(?<![a-zA-Z])rot(?![A-Za-z])/,'')

要提供一个想法,请使用正则表达式(例如此处的规则rot)在存在非字母的前后删除rot