这是我当前的代码.感谢@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模板网!
discord.js v12:我如何等待 DM 频道中的消息?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(如何让我的机器人提及发出该机器人命令的人)
如何修复必须使用导入来加载 ES 模块 discord.jsHow 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:修复“找不到 FFMPEG"Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
使用 discord.js 加入 discord 服务器时的欢迎消息Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)