问题描述
这是我当前的代码.感谢@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条bot消息时,该bot不会删除该消息,并带有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并解决DiscordAPIError的bulkDelete问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!