簡體   English   中英

如何使用discordjs忽略機器人特定的消息

[英]How to ignore bot specific messages using discordjs

有沒有辦法忽略特定的機器人消息?

我正在嘗試這個。

if (message.author.bot) return;

但這忽略了所有機器人消息,我只想忽略特定消息,而不是全部。

嘗試類似的東西

//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;

或使用過濾器,切換

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

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

上面的代碼僅忽略消息是否完全按照列表​​中的類型輸入(區分大小寫)。 如果你希望它不區分大小寫,改變message.contentmessage.content.toLowerCase()並確保您的列表只包含小寫版本。

如果你想檢查每個單詞,你可以簡單地使用循環遍歷每個單詞。

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

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

以特定短語開始每個列入黑名單的消息,或在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM