久久久久久久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 服務器時的歡迎消息)
    主站蜘蛛池模板: 欧美日韩综合 | 国产精品久久久 | 综合一区 | 91在线看网站| 精品国产成人 | 国产日韩精品久久 | 久久国 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 韩日精品在线观看 | 蜜桃视频在线观看免费视频网站www | 国内毛片毛片毛片毛片 | 国产一区二区三区在线观看免费 | 黄a免费网络 | 久久综合久久自在自线精品自 | 精品视频www | 九九热最新地址 | a免费视频 | 国产伦精品一区二区三区高清 | a在线观看| 福利视频一区二区 | 国产精品99一区二区 | 国产精品久久久亚洲 | 色综合久久天天综合网 | 日韩在线h | 国产一级毛片视频 | 精品国产91亚洲一区二区三区www | 欧美日韩成人网 | 精品伊人久久 | 天堂网色 | 成年人黄色一级毛片 | 免费在线观看一区二区 | 亚洲va在线va天堂va狼色在线 | 日本三级电影免费观看 | 国产一区二区三区在线 | 亚洲高清一区二区三区 | 别c我啊嗯国产av一毛片 | 亚洲一区 中文字幕 | 中文字幕专区 | 久久国产视频播放 | 欧美二区在线 | 欧美在线一区二区三区 |