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

128 Views Asked by At
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.

2

There are 2 best solutions below

0
On BEST ANSWER

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]
0
On

You named the argument in the .then() function message, so it overwrote the first message object. You should rename it to messages because you reference that name on the same line.

A few tips:

  • args[0] will be a string, not a number, so it won't take any effect. You can use the unary operator to convert it.

  • There's no point fetching the messages, because you can just input a number to the bulkDelete() function

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