Home > Blockchain >  Clear command is not deleting messages correctly discord.js v13
Clear command is not deleting messages correctly discord.js v13

Time:06-15

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

CodePudding user response:

  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!

CodePudding user response:

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)
  • Related