Search code examples
javascriptdiscord.js

bulkDelete just author messages?


Is there any way to use "bulk Delete" just to delete messages from the author of the command?

I tried this but the time to delete is very slow.

const Discord = require("discord.js")

module.exports = {
    name: "cl", // Coloque o nome do comando do arquivo
    aliases: ["clean", "c"], // Coloque sinônimos aqui

    run: async(client, message, args) => {

        await message.channel.messages.fetch({
            limit: 100
          }).then((msgCollection) => {
            msgCollection.forEach((msg) => {
              if(msg.author.id == message.author.id) {
                msg.delete()
              }
            }
          )});
        }
}

Solution

  • The simplest option is to fetch all available messages, filter, then bulk delete.

    const allMessages = await message.channel.messages.fetch({limit: 100});
    const authorMessages = allMessages.filter(m => m.author.id === message.author.id);
    await message.channel.bulkDelete(authorMessages);