Search code examples
javascriptdiscord.jsbulk-delete

Did discord change the "bulkDelete" variable in java script?


module.exports = {
    name: 'clear',
    description: 'clear certain amount of messages',
    async execute(message, args){

        if(!args[0]) return message.reply("please enter the amount of messages");
        if(isNaN(args[0])) return message.reply("please type a number");
        if(args[0] > 100) return message.reply("you can not delete more than 100 messages at a time.")
        if(args[0] < 1) return message.reply("Number has to be more than 1")

        await message.channel.messages.fetch({limit: args[0]}).then(message =>{
            message.channel.bulkDelete(messages);
        });
    }
}

Every time I run it, an error occurs:

Cannot read property 'bulkDelete' of undefined

i have no idea what i did wrong. I checked the code in the video as well as went on discord.js.org to see if they had a different solution. If anyone can help, I'll really appreciate it.


Solution

  • messages.fetch resolves to a Collection of messages. You need to pass that array to the bulkDelete method on the original message.

    Also, since you're using await, you might as well remove the .thens entirely.

    const messages = await message.channel.messages.fetch({limit: args[0]});
    await message.channel.bulkDelete(messages);
    

    But it would be easier to just pass the number of messages to delete.

    message.channel.bulkDelete(args[0]);
    

    I'd also recommend putting the args[0] into a variable beforehand, for better readability.

    async execute(message, args){
      const numToDelete = args[0];
      // continue using numToDelete, not args[0]