Max H.
Max H.

Reputation: 45

Promise remains unresolved even after handling all cases

I have been writing on a Discord.JS Bot for quite a bit now, and every now and then it seems to throw me a random error / warning in console after executing one of its chat commands (specifically !clear).

Now, as I already stated, the message I get in my console is a warning, not an actual error, so that's not the main problem I have;

My problem lies in the execution of the command on Discord's side: Because of the unresolved, rejected promise, it will not execute !clear at all, leaving all messages including the command itself behind. Here's a snippet of my code:

if (member.hasPermission("MANAGE_MESSAGES")) {
    channel.fetchMessages({ limit: 100 })
        .then(messages => {
            console.log(`Deleting ${messages.size} messages...`);
            channel.bulkDelete(messages).then(res => {}, err => {});
            channel.sendEmbed({
                // Success Message
            }).then(msg => msg.delete(10000), err => console.log(err));
        }, err => { console.log(err) })
} else {
    channel.sendEmbed({
        // Permission Message
    }).then(msg => msg.delete(10000), err => { console.log(err) });
}

As you can see, I resolved both the success and the failure state of every Promise, yet I will still see the following warning in console:

(node:14768) Error: Bad Request Sometimes also throws Not Found

-- Stack Trace that only includes internal Node.JS errors --

(node:14768) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

-- More Stack Trace not regarding any of my own code --

If any of you need additional code provided to answer the question, feel free to ask me and I will do so. Also, I can't +rep answers yet, but I always appreciate 'em :)

Upvotes: 0

Views: 2737

Answers (1)

idbehold
idbehold

Reputation: 17168

You aren't handling the msg.delete(10000) rejection. You should handle it like this:

channel.sendEmbed({
  // Success Message
}).then(msg => msg.delete(10000)).catch(err => console.log(err));

Upvotes: 1

Related Questions