問題描述
這是我當前的代碼.感謝@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 有兩個問題("!" || "." || ">"));
:
msg.content.startsWith("!" || "." || ">")
只會根據第一個真實的語句進行評估:"!"代碼>.
String#startsWith
只需要一個模式,因此您必須將該調用拆分為三個調用.為方便起見,讓我們將這些檢查的結果分配給單個變量:
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模板網!