如何忽略特定于机器人的消息Discord.js

时间:2019-08-23 16:23:40

标签: javascript node.js bots discord.js

有没有办法忽略特定的漫游器消息,我使用了以下方法:

if (message.author.bot) return;

但这会忽略该机器人发送的所有消息,我只想忽略该机器人的特定消息。

3 个答案:

答案 0 :(得分: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;

或使用过滤器,切换

答案 1 :(得分:1)

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

答案 2 :(得分:0)

以特定短语开始每条列入黑名单的邮件,或将其列表保存在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