简体   繁体   English

清除命令未正确删除消息 discord.js v13

[英]Clear command is not deleting messages correctly discord.js v13

When I want to clear messages with the code below:当我想使用以下代码清除消息时:

await message.channel.messages.fetch({limit: args[0]}).then(messages =>{
message.channel.send(`Deleting Messages...`).then(msg => {
setTimeout(() => msg.delete(), 500)
})
message.channel.bulkDelete(messages);
});

It works, But when I say: "${prefix}clear 3", It deletes 2 messages.它有效,但是当我说:“${prefix}clear 3”时,它会删除 2 条消息。 or when the messages are for more than the last 14 days, it cannot delete the messages.或者当消息超过过去 14 天时,它不能删除消息。

List:列表:

  1. I want the bot to delete the specific number that was specified in args.我希望机器人删除 args 中指定的特定数字。
  2. and when the requested amount of messages was for the last 14 days, it says "Sorry, I can't delete those messages because they are for the past 14 days."当请求的消息量是过去 14 天时,它会显示“抱歉,我无法删除这些消息,因为它们是过去 14 天的。”

Extra Note: I am using discord.js v13 and node.js v16额外说明:我使用的是 discord.js v13 和 node.js v16

  1. Your bot also counts your command as a message, and removes it.您的机器人还将您的命令视为一条消息,并将其删除。 That's why it only removes 2 other messages.这就是为什么它只删除另外 2 条消息。 To prevent that you will have to add a filter or remove your command first using message.delete()为防止您必须先使用message.delete()添加过滤器或删除命令
  2. Channel.bulkDelete() is not removing messages that are older than 2 weeks by default! Channel.bulkDelete()默认不会删除超过 2 周的消息! To enable filterOld parameter you'll have to use Channel.bulkDelete(number, true) and you'll be able to delete these messages as well!要启用filterOld参数,您必须使用Channel.bulkDelete(number, true)并且您也可以删除这些消息!

First of all, your command is a message thats why it deletes less that the amount Second of all, bots cannot delete messages that are older than 2 weeks third of all, you cannot delete more than 100 messages.首先,您的命令是一条消息,这就是为什么它删除的数量少于该数量。其次,机器人无法删除超过 2 周的消息第三,您不能删除超过 100 条消息。 so you can do:所以你可以这样做:

const amount = args[0]

if (amount > 100 || amount < 1) {
 return message.channel.send({content: `I cannot delete 100 messages or more`})
        }
const messages = await message.channel.messages.fetch({
            limit: amount + 1,
        }); 
await message.channel.bulkDelete(messages, true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM