Discord.js bot, how to purge without pins?

629 Views Asked by At

This is my purge command, I want my purge to ignore pins, anyone knows how could I do it?

This is what I got for now:

    } else if (message.content.toLowerCase().startsWith(`${PREFIX}purge`)) {
        if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.channel.send("You don\'t have permissions to do this command")
        if (!message.guild.me.hasPermission('MANAGE_MESSAGES')) return message.channel.send("I don\'t have permissions to do this command")
        if (!args[1]) return message.channel.send("You need to specify a number of messages to purge")
        if (isNaN(args[1])) return message.channel.send("That isn\'t a valid amount of messages to purge")
        if (args[1] > 100 || args[1] < 2) return message.channel.send("Make sure that your amount is ranging 2 - 100")
        try {
            await message.channel.bulkDelete(fetch(message).filter(message => !message.pinned)).size
        } catch {
            return message.channel.send("You can only bulk delete messages within 14 days of age")
        }
        var embed = new Discord.MessageEmbed()
        .setAuthor(`${message.author.username} - (${message.author.id})`, message.author.displayAvatarURL())
        .setThumbnail(message.author.displayAvatarURL())
        .setColor('#FFAE00')
        .setDescription(`
**Deleted:** ${args[1]}
**Action:** Purge
**Channel:** ${message.channel}
**Time:** ${moment().format('llll')}
        `)
        const channel = message.guild.channels.cache.find(c => c.name === "audit-log")
        channel.send(embed)
    }
})

await message.channel.bulkDelete(fetch(message).filter(message => !message.pinned)).size

BTW this last line doesn't work but it is what I think I need to modify

1

There are 1 best solutions below

0
On

Use this:

await message.channel.bulkDelete(
  (await message.channel.messages.fetch({limit: args[1]}))
    .filter(m => !m.pinned)
)

Note that if there are pinned messages, it will delete less messages than args[1].