久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Discord.js//過濾消息startsWith 并解決bulkDelete 的Dis

Discord.js // Filtering message startsWith and work around DiscordAPIError for bulkDelete(Discord.js//過濾消息startsWith 并解決bulkDelete 的DiscordAPIError)
本文介紹了Discord.js//過濾消息startsWith 并解決bulkDelete 的DiscordAPIError的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是我當前的代碼.感謝@André Dion 的幫助.

This is my current code. Credit goes to @André Dion for the help.

  if (message.channel.type == 'text') {
    message.channel.fetchMessages().then(messages => {
        const botMessages = messages.filter(msg => msg.author.bot) 
        message.channel.bulkDelete(botMessages);
        messagesDeleted = botMessages.array().length; // number of messages deleted

        // Logging the number of messages deleted on both the channel and console
        message.channel.send("Deletion of messages successful. Total messages deleted: " + messagesDeleted);
        console.log('Deletion of messages successful. Total messages deleted: ' + messagesDeleted)
    }).catch(err => {
        console.log('Error while doing Bulk Delete');
        console.log(err);
    });
}

當用戶輸入!clearMessages"時,它會運行此代碼并僅刪除來自機器人的消息.我想添加一個功能,它還可以刪除以 !/./> 開頭的用戶消息(這些消息不僅可以來自用戶,也可以來自機器人),所以我嘗試使用 const botMessages 將行編輯為:const botMessages = messages.filter(msg => msg.author.bot && msg.content.startsWith("!" || "." || ">"));不行.你能指出我哪里出錯了,我該如何解決這個問題?

When the user enters "!clearMessages" it runs this code and deletes only messages from bots. I would like to add a feature where this also deletes messages from users that starts with !/./> (these messages can be from users not only bots), so I tried editing the line with the const botMessages to this: const botMessages = messages.filter(msg => msg.author.bot && msg.content.startsWith("!" || "." || ">")); but that didn't work. Can you please point out where I'm going wrong and how I can fix this?

我注意到的另一個問題是,當只有 1 條機器人消息時,機器人不會刪除該消息并出現 DiscordAPIError,表示您必須提供至少 2-100 條消息才能刪除.有解決辦法嗎?

Another issue I noticed is that when there is only 1 bot message the bot doesn't delete the message and comes up with an DiscordAPIError, saying that you must provide at least 2-100 messages to delete. Is there a work around this?

謝謝.

推薦答案

const botMessages = messages.filter(msg => msg.author.bot && msg.content.startsWith 有兩個問題("!" || "." || ">"));:

  1. msg.content.startsWith("!" || "." || ">") 只會根據第一個真實的語句進行評估:"!".String#startsWith 只需要一個模式,因此您必須將該調用拆分為三個調用.為方便起見,讓我們將這些檢查的結果分配給單個變量:

  1. msg.content.startsWith("!" || "." || ">") is only going to evaluate against the first truthy statement: "!". String#startsWith only takes a single pattern, so you'll have to split that call into three calls. Let's assign the result of these checks into a single variable for convenience:

const isCommand = msg.content.startsWith("!") || msg.content.startsWith(".") || msg.content.startsWith(">");

  • 您想要過濾掉機器人用戶發出的或看起來像命令的消息.當前,您的邏輯已編寫,因此機器人發出的消息 看起來像命令被過濾,這是錯誤的(機器人不會發出任何命令).上述添加的正確檢查是:

  • You want to filter out messages that are issued by bot users or that look like a command. Currently your logic is written so that messages that are issued by bots and look like a command are filtered, which is wrong (bots won't be issuing any commands). The correct check with the above additions would be:

    const botMessages = messages.filterArray(msg => {
        const isCommand = msg.content.startsWith("!") || msg.content.startsWith(".") || msg.content.startsWith(">");
    
        return msg.author.bot || isCommand;
    });
    

  • 更正您的過濾器邏輯應該會修復您的 DiscordAPIError 異常,但要確保沒有發出錯誤的調用,您應該保護 bulkDelete 調用:

    Correcting your filter logic should fix your DiscordAPIError exception but to ensure no bad calls are being issued, you should guard the bulkDelete invocation:

    if (botMessages.length > 1) {
        message.channel.bulkDelete(botMessages);
    } else if (botMessages.length) {
        botMessages[0].delete();
    } else {
        // nothing to delete
    }
    

    這篇關于Discord.js//過濾消息startsWith 并解決bulkDelete 的DiscordAPIError的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

    相關文檔推薦

    discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
    how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
    How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
    How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
    Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
    Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
    主站蜘蛛池模板: 成人依人 | av中文在线 | 日日日色 | 在线视频一区二区三区 | 日韩av一二三区 | 一区二区三区免费在线观看 | 国产精品123区 | 久久精品色欧美aⅴ一区二区 | 99精品99久久久久久宅男 | 日韩精品一区二区三区视频播放 | 国产成人精品免高潮在线观看 | 久久久久国产 | 久久国产精品视频 | 成人a在线观看 | 日韩精品久久一区二区三区 | 久久丝袜视频 | 精品9999 | 色天堂影院 | 亚洲男人的天堂网站 | 成人自拍视频网站 | 亚洲三级在线观看 | 在线观看av网站永久 | 国产激情一区二区三区 | 欧美成人免费在线 | 玖玖操 | 午夜精品一区二区三区三上悠亚 | 欧美黑人又粗大 | 日韩一区二区三区av | 日韩综合在线视频 | 天天躁日日躁狠狠躁白人 | 亚洲精品久久久久中文字幕欢迎你 | 国产精品久久久久久久久污网站 | 国外成人在线视频网站 | 国产 欧美 日韩 一区 | 亚洲一区 中文字幕 | 国产精品视频一 | 国产精品视频中文字幕 | 波多野结衣亚洲 | h片在线播放 | 日韩一区和二区 | 伊人免费网 |