当我使用该命令时,如果有超过 14 天的消息并且有几条不超过 14 天的消息,我可以删除那些不超过 14 天的消息,但仍然收到 14 天错误.我希望它仅在有 0 条不超过 14 天的消息时才返回错误。 请帮忙。
我的代码:
文件名:purge.js
module.exports = {
name: "purge",
aliases: ['clear'],
category: "moderation",
permissions: "MANAGE_MESSAGES",
description: "Deletes messages from a channel.",
usage: "purge [amount of messages] [ignore pinned messages]",
execute: async (message, args) => {
if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.channel.send("You don't have enough perms! You require: [MANAGE_MESSAGES]")
message.delete()
if (!args[0]) {
message.channel.bulkDelete(100)
.then().catch(e => {
message.channel.send(e.message)
})
.then(messages => message.channel.send(`**Succesfully deleted ${messages.size - 1} messages**`).then(msg => msg.delete({ timeout: 5000 }))).catch(() => null)
return
}
else if (args[0] === "true") {
const fetched = await message.channel.messages.fetch({ limit: 100 });
const notPinned = fetched.filter(fetchedMsg => !fetchedMsg.pinned);
await message.channel.bulkDelete(notPinned)
.then().catch(e => {
message.channel.send(e.message)
})
.then(messages => message.channel.send(`**Succesfully deleted ${messages.size - 1} messages.**`).then(msg => msg.delete({ timeout: 5000 }))).catch(() => null)
return
}
else if (parseInt(args[0]) > 100) {
return message.channel.send("**Please Supply A Number Less Than 100!**");
}
else if (parseInt(args[0]) < 1) {
return message.channel.send("**Please Supply A Number More Than 1!**");
}
if (args[1] === "true") {
const fetched = await message.channel.messages.fetch({ limit: args[0] });
const notPinned = fetched.filter(fetchedMsg => !fetchedMsg.pinned);
await message.channel.bulkDelete(notPinned)
.then().catch(e => {
message.channel.send(e.message)
})
.then(messages => message.channel.send(`**Succesfully deleted ${messages.size - 1} messages**`).then(msg => msg.delete({ timeout: 5000 }))).catch(() => null)
return
}
else {
message.channel.bulkDelete(args[0])
.then().catch(e => {
message.channel.send(e.message)
})
.then(messages => message.channel.send(`**Succesfully deleted ${messages.size - 1} messages**`).then(msg => msg.delete({ timeout: 5000 }))).catch(() => null)
return
}
},
};
答案 0 :(得分:2)
bulkDelete 可以采用名为 filterOld 的第二个参数。 顾名思义,这使得 discord.js 忽略所有超过 14 天的消息。
示例:
message.channel.bulkDelete(notPinned, true)
message.channel.bulkDelete(args[0], true)
答案 1 :(得分:0)
使用message.channel.bulkDelete(<amount>, true)