【问题标题】:How to make a bad words filter in discord.js v13如何在 discord.js v13 中制作坏词过滤器
【发布时间】:2022-10-24 03:36:24
【问题描述】:

我试图在 discord.js v13 和 node.js v16 中使用我的机器人过滤坏词。

当我发送“word1”消息时,它可以正常工作并删除该消息。但是当我发送例如:“Hey word1”时,它不会删除消息。

我试过的:

const args = message.content.split(/ +/);
if ((message.guild.id = 'GUILD_ID')) {
  const bad = ['word1', 'word2'];
  if (bad.includes(args[0].join(' '))) {
    message.delete();
  } else {
    return;
  }
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    这是因为您检查数组是否包含字符串"Hello word1"

    您可以使用 Array#some() 和回调函数来检查字符串是否包含数组中的任何这些单词:

    function containsBadWords(str) {
      const badWords = ['word1', 'word2']
    
      if (badWords.some(word => str.toLowerCase().includes(word))) 
        return 'String contains bad words'
      else 
        return 'No bad words found'
    }
    
    console.log(containsBadWords('word1'))
    console.log(containsBadWords('Some other text that includes word2 here'))
    console.log(containsBadWords('No bad words here'))
    // make sure you don't use single '=' here!
    if (message.guild.id === 'GUILD_ID') {
      const bad = ['word1', 'word2']
      if (bad.some(word => message.content.includes(word)))
        message.delete()
      else
        return
    }
    

    【讨论】:

    • 你好,谢谢分享。我用一些编辑替换了您与我共享的代码:js if (message.guild.id === '847071265100791849'){ const fosh = ['words1', 'word2']; if (fosh.some(word => args[0].join(' ').includes(word))){ message.delete(); } else { return; } 但它返回错误:err args[0].join is not a function
    • 啊,对,args[0]是一个字符串,应该是args.join(),或者干脆是message.content。我只是用它来更新我的答案。
    【解决方案2】:

    数组包含不会为您的 Hello world1 字符串返回 true。在您的情况下,您想查找数组是否通过子字符串包含该值。这不是 array.includes 方法的工作方式。看看这个答案In javascript, how do you search an array for a substring match

    【讨论】:

    • 这与我的问题无关。
    • 什么不相关?我描述了你的问题,为什么你没有得到正确的结果
    猜你喜欢
    • 2022-01-24
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多