【问题标题】:How to ignore bot specific messages using discordjs如何使用 discordjs 忽略特定于机器人的消息
【发布时间】:2019-12-28 23:52:33
【问题描述】:

有没有办法忽略特定的机器人消息?

我正在尝试这个。

if (message.author.bot) return;

但这忽略了所有机器人消息,我只想忽略具体消息,而不是所有消息。

【问题讨论】:

  • 通过设置一些条件

标签: javascript node.js bots discord.js


【解决方案1】:

试试类似的东西

//noReply should contain unanswered messages    
const noReply = [ 'messages noReply','noReply exp','noReply hello','nop noReply','xxx','one a one','lazy' ];
const mab = message.content;
if (noReply.some(msgs => msgs == mab)) return;

或使用过滤器,切换

【讨论】:

  • 这是如何工作的?您从未使用过message.content,所以我在这里有点困惑。
  • 对不起,我错了。在 message.author.bot 中正确的是 message.content
【解决方案2】:
const blacklist = ['test', 'hello', 'world']

if(blacklist.includes(message.content)) return 

上面的代码只在消息完全按照列表​​中的类型输入时才会忽略(区分大小写)。如果您希望它不区分大小写,请将 message.content 更改为 message.content.toLowerCase() 并确保您的列表仅包含小写版本。

如果你想检查每个单词,你可以简单地使用一个循环遍历每个单词。

const blacklist = ['test', 'hello', 'world']

const words = message.content.split(' ')
words.forEach(word => {
    if(blacklist.contains(word)) return
})

【讨论】:

    【解决方案3】:

    以特定短语开始每条列入黑名单的消息,或在blacklist 数组中保留它们的列表

    // Inside of message event
    const blacklist = ['noreply', 'dnr']; // These are case sensitive
    if (message.author.bot) {             // and may contain spaces
    
      // Otherwise anyone could start their message with "noreply" and be ignored
      if (blacklist.some(phrase => message.content.startsWith(phrase))) return;
      // .... continue with rest of code
    }
    
    

    注意:不要只使用blacklist.some(message.content.startsWith),因为它会根据blacklist中短语的索引跳过一些单词

    Array.prototype.some

    String.prototype.startsWith

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2020-11-10
      • 2022-12-18
      相关资源
      最近更新 更多