Mihal
Mihal

Reputation: 3

Discord.js Throw exception in .catch

Because the discord doesn't support to delete messages older than 14 days. I want to create an exception to the error, when the bot is trying to delete old messages,but the throw in .catch doesn't work and i don't know why.

This is my code:

    try{
      message.channel.bulkDelete(200, false)
      .catch(error =>{
           throw 1;
      });
    message.channel.send(“Deleted 200 messages”);
    }catch(error){
     message.channel.send(“cant delete messages”);

    }

I know that i can do this bulkDelete(200, true) but that wont throw exception when it is trying to delete the old messages.

Upvotes: 0

Views: 1826

Answers (1)

Legix
Legix

Reputation: 51

Try to do this

try {
  await message.channel.bulkDelete(100, false);
  message.channel.send("Deleted 100 messages");
} catch(error) {
  message.channel.send("Can't delete messages");
}

bulkDelete is a promise so you have to put it in an async function and await it or use the .then syntax. Also it can delete only 100 messages and returns an error with more.

I'm not sure why you want to throw 1 when you get an error but if you need it you can put it in the catch statement after sending the message

Upvotes: 1

Related Questions