【问题标题】:A Discordjs Chat FilterDiscordjs 聊天过滤器
【发布时间】:2019-04-02 22:19:27
【问题描述】:

我正在制作一个 discord.js 机器人,我很好奇如何为其添加聊天过滤器。正如用户所说的f***,它会自动删除。

【问题讨论】:

标签: json discord discord.js


【解决方案1】:

我创建了一个简单的方法,例如

let badwords = ['word1', 'word2']
client.on('message', async (message) => {
            if (badwords.includes(message.content.toLowerCase())) {
                message.delete();
                message.channel.send(`HEY! you cant send that here`);
            }
        })

这是我用于我的机器人的过滤器。它首先将消息转换为小写,然后在数组中搜索单词。如果它发现单词与小写消息中的单词匹配,则删除该消息。顺便说一句,数组中的单词必须小写才能起作用。

【讨论】:

    【解决方案2】:
    bot.on("message", async message => {
    
        //Define the words that should be deleted after a successfull detection
        let blacklisted2 = ['Some', 'Random', 'word'];
    
        //Set foundInText to false
        let foundInText2 = false;
    
        //Check for the blacklisted words in blacklisted2
        for (var i in blacklisted2) {
    
            if (message.content.toLowerCase().includes(blacklisted2[i].toLowerCase()))
    
            //If a blacklisted word has been detected in any sentences, it's going to set foundInText2 to true.
                foundInText2 = true;
    
        }
    
        //If something has been detected, delete the message that has a blacklisted word inside of it.
        if (foundInText2) {
    
            //Delete the message.
            message.delete();
    
            //Then do whatever you want
    
        }
    
    });
    

    告诉我 如果一切正常。如果您有任何问题,请在此处联系/回答我。

    【讨论】:

      【解决方案3】:
      const blacklisted = ["word1", "word2", "word3"] // and continue doing this until you have entered all your desired words
      client.on("message", message => {
        let shouldDelete = false
        for(word in blacklisted){
          if(message.content.includes(word)) shouldDelete = true
        }
        if(shouldDelete) message.delete()
      });
      

      如果我在哪里搞砸了,请告诉我。我就是这样做的。 (未测试)

      【讨论】:

        【解决方案4】:

        这是一个保证工作的聊天过滤器!

        bot.on("message", async message => {
        
          let blacklisted = ['yourbadword1', 'yourbadword2', 'and some more bad words :3'];
        
        let foundInText = false;
        for (var i in blacklisted) {
          if(message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
        }
        
        if(foundInText) {
          message.delete();
          message.channel.send("Stop using this bad word!")
        }
        });
        

        【讨论】:

          【解决方案5】:

          你可以用一组单词来做到这一点,

          var profanities = ["test1", "test2", "..."];
          

          然后在你的 bot.on 消息处理程序中,或者你如何使用来处理消息,

          bot.on('message', async message => {
              let msg = message.content.toLowerCase();
              let Admin = message.guild.roles.find('name', "Admin"); // Change this to meet what you want
              let General = message.guild.channels.find(`name`, "general"); // All text channels are lowecase. Change this to meet what you want
              for (x = 0; x < profanities.length; x++) {
                  if(message.member.roles.has(Admin.id) || message.channel.id === General.id) return; // if you changed the name of the variables above, change these too.
                  if (msg.includes(profanities[x])){
                      await message.reply("You cannot say that here!")     
                      message.delete()
                      // Your code here
                      return;     
                  }
              }
          });
          

          编辑: 这是非常基本的,它不会寻找替代字母,例如 $、@ 或数字/空格,除非您直接在其中编码,您可以拥有一个单词列表,然后让控制台记录每个带有替代字母的单词。

          【讨论】:

          • 谢谢!还有一个问题,有没有办法让管理员绕过它?或者有没有办法让它忽略某个频道?
          • 是的,你可以用这段代码检查发件人是否有一定的角色,在“let msg = ...”下面添加它让role = message.guild。 roles.find('name', "区分大小写的角色名称") message.member.roles.has(role.id),为了您的方便,我更新了帖子
          • 我也忘了说,你也可以查一下频道!两者都已在上面的代码中实现,两个新变量是您更改的内容。如果您更改它们的名称,请确保在新的 if 语句中也这样做
          【解决方案6】:
          client.on('message', message => {
              message.delete(message.content.replace(/asshole/gi))
              .catch(console.error);
          });
          

          这只是一个你可以做的例子,还有一个文本数组。

          【讨论】:

            猜你喜欢
            • 2018-09-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多