Paul WOISARD
Paul WOISARD

Reputation: 35

Delete message in a specific channel

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.

Upvotes: 0

Views: 3736

Answers (2)

PLASMA chicken
PLASMA chicken

Reputation: 2785

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

Upvotes: 2

Federico Grandi
Federico Grandi

Reputation: 6816

If you want to check the channel with the id, you should write:

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

Upvotes: 1

Related Questions