简体   繁体   中英

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. or when the messages are for more than the last 14 days, it cannot delete the messages.

List:

  1. I want the bot to delete the specific number that was specified in 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."

Extra Note: I am using discord.js v13 and 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. To prevent that you will have to add a filter or remove your command first using message.delete()
  2. Channel.bulkDelete() is not removing messages that are older than 2 weeks by default! To enable filterOld parameter you'll have to use Channel.bulkDelete(number, true) and you'll be able to delete these messages as well!

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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