Ad

Delete Message In A Specific Channel

- 1 answer

I'm trying to delete an old message in a specific channel with my bot.
The code below doesn't work and I don't why.

if (msg.channel == channelDLid) {
  msg.delete(6000);
}

The code is executed but it doesn't do anything.

Ad

Answer

You can use Channel#bulkDelete which allows you to delete up to 2-week old messages. To only delete specific messages you can use Channel#fetchMessages like:

const messages = await message.channel.fetchMessages({ limit: 100}) // Fetch last 100 messages
  .then(msgs => msgs.first(msgs.size - 3)) // Remove the last 3 messages out of the collection to delete

message.channel.bulkDelete(messages, true);
Ad
source: stackoverflow.com
Ad