Discord.js Bulk Delete command

Tags: , , ,



I am trying to add a bulk delete command to my bot but when I type how many messages I want to delete, I get the following error:

TypeError [MESSAGE_BULK_DELETE_TYPE]: The messages must be an Array, Collection, or number.

Here’s the code:

else if (isVallidCommnad(message, "delete")){
            if (!message.member.hasPermission('KICK_MEMBERS'))  return message.channel.send("You cannot delete messages :/");
            if(!args[0]) return message.reply(" How many messages do you want to delete (limit 99)");
            if(parseInt(args[0]) > 99) return message.reply("You can't delete more than 99 messages at once dude!!");
            
            message.channel.bulkDelete(parseInt(args[0]) + 1 ).then(message =>{
                message.channel.send(`Cleared ${args[0]} messages!`).then (message =>message.delete({timeout: 300}));
                message.react("👌")
            }).catch((err) =>{
                console.log(err)
                return message.reply("An error occurred!")
            })

Answer

const deleteCount = parseInt(args[0], 10);

if (!deleteCount || deleteCount < 1 || deleteCount > 100) return;

message.channel.bulkDelete(deleteCount + 1).catch(error => message.reply(`Couldn't delete messages because of: ${error}`));

To use the parseInt(), you have to add the decimal base, so 10 -> parseInt(args[0], 10);. The snippet of code I have put above is working well.



Source: stackoverflow