Ad2017
Ad2017

Reputation: 19

How to delete n messages from a channel?

I am working on a discord bot. I want to implement a feature like Tatsumaki's t!prune 5 (deletes 5 messages from the history).

Okay, lemme show you what I mean one way or another:

msg.channel.delete(2); // 2 is the number of messages being deleted.
                       // this is not a real function, just an example

Is there such a thing like what I showed?

Upvotes: 0

Views: 257

Answers (1)

Gilles Heinesch
Gilles Heinesch

Reputation: 2980

Try to use the following

const fetchedMessages = await msg.channel.fetchMessages();
const amount = 50 // number of messages that should be deleted (max 50 otherwise you have to change the option limit property for .fetchMessages())



for (let i = 0; i < amount; i++) {
await fetchedMessages[i].delete()
}

You could also use the .bulkDelete() function:

await msg.channel.bulkDelete(AMOUNT)

Upvotes: 2

Related Questions