I want to make a bulk-clear command for my bot. I tried this code:
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); });
But it doesn’t work correctly. when I run !clear 3
, It deletes 2 messages.
- Notes: I am using discord.js v13 and node.js v16
Advertisement
Answer
- 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()
Channel.bulkDelete()
is not removing messages that are older than 2 weeks by default! To enablefilterOld
parameter you’ll have to useChannel.bulkDelete(number, true)
and you’ll be able to delete these messages as well!