Samurai
Samurai

Reputation: 23

UnhandledPromiseRejectionWarning: TypeError: message.channel.bulkDelete is not a function

For some reason, I get that error when using bulkDelete() It was working recently. (I didn't update it.) Code:

module.exports = {
    name: 'clear',
    description: 'Clears messages',
    async execute(message, args) {
        if (!args[0]) return message.reply("Specify Amount");
        if (isNaN(args[0])) return message.reply("Enter a real number");
        if (args[0] > 100) return message.reply("100 is the max number of messages to clear");
        if (args[0] < 1) return message.reply("1 is the minimum number of messages to clear");
        var clrMsg = (args[0]) - -1;   // (using +1 puts a 1 infront of the number)
        if (clrMsg == 101) {
            clrMsg = 100;
        }
        await message.channel.messages.fetch({
            limit: args[0]
        }).then(messages => {
            message.channel.bulkDelete(clrMsg);
        });
    }
}

UPDATE: I'm getting the same error when it was just working. I retyped the entire code and it still returns that error.

Upvotes: 0

Views: 208

Answers (2)

Samurai
Samurai

Reputation: 23

Still no error with the code. I retyped the exact same thing and it works. Probably a bug with discord.js. New code: message.channel.bulkDelete(clrMsg);

Upvotes: 0

Apoorva Chikara
Apoorva Chikara

Reputation: 8773

There seems two issues with the code: the way you are defining method and using then with await. You should do something like this:

module.exports = {
    name: 'clear',
    description: 'Clears messages',
    execute: async (message, args) => {
        if (!args[0]) return message.reply("Specify Amount");
        if (isNaN(args[0])) return message.reply("Enter a real number");
        if (args[0] > 100) return message.reply("100 is the max number of messages to clear");
        if (args[0] < 1) return message.reply("1 is the minimum number of messages to clear");
        var clrMsg = (args[0]) - -1;   // (using +1 puts a 1 infront of the number)
        if (clrMsg == 101) {
            clrMsg = 100;
        }
    try {
        const messages =  await message.channel.messages.fetch({
            limit: args[0]
        })
            message.channel.bulkDelete(clrMsg); // it seems message doesn't have the function, you might need messages instead
        } catch (e) { 

           console.log(`Error is: ${e}`);
        }
       
    }
}

Always use try/catch with async/await.

Upvotes: 1

Related Questions