Code is functional, but spits out errors in my console when ran

The following code is a command to clear 15 messages from a user, and it’s entirely functional, but when I use the command to clear messages, I start getting flooded with error messages in my console.

Example: http://prntscr.com/qsmf9f

The code for the command is:

const Discord = require('discord.js')
const EmbedHelper = require('../include/embeds.js')
const Logger = require('../include/globals.js')
module.exports.run = async (client, message, args, config) => {
    message.channel.fetchMessages({ limit: 15 }).then(messages => {
        let array = messages.array();
        array = array.filter(m => m.author.id === client.user.id);
        array.length = 15 + 1;
        array.map(m => m.delete().catch(console.log))
    })
    log(`Deleting 15 messages from chat`)
}

Help!

You’re trying to overwrite the length of the array.

Use message.channel.bulkDelete(array) to delete the messages. Also note that bots can’t delete messages more than 2 weeks old.

1 Like