Unhandled promise rejection | restart command

5.2k Views Asked by At
const { MessageEmbed, Guild } = require("discord.js");
const db = require('quick.db');

module.exports = {
    config: {
        name: "restart",
        category: "moderation",
        aliases: ["r"],
        description: "restarts the bot",
        accessableby: "Administrator",
        usage: "restart",
    },
    run: async (bot, message, args) => {
    if (!message.author.id === '556247341838106624' ) {
        return message.channel.send(`you cant use this!`)
    }
    await message.guild.channels.cache.find(channel => channel.name === "restart").id
   
    process.exit();

        
    }
}

ERROR:

(node:6772) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:6772) [DEP0018] 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.

1

There are 1 best solutions below

1
On

How familiar are you with promises? A promise can be in one of three states:

  • Pending
  • Fulfilled
  • Rejected

    Pending is when the computation is being made (say you try to fetch something from a database) It's neither fulfilled nor rejected.
    Fulfilled is if the operation was successful. For example, the data was fetched.
    Rejected is if something went wrong. The operation failed.

    Now, looking back at the warning, it tells you that silently unhandled promise rejections are deprecated. In a future node version, this will result in your program crashing.

    There are two ways of handling promises. Either by chaining .then to the promise, or by using the async await method.

Looking at the .then method first:

somePromise.then((data) => {
  console.log(data);
}).catch((err) => {
  console.error(err.message);
});

In this example, we are chaining .then().catch(); to the promise. If the promise is fulfilled (meaning everything went well), the operation inside .then() will be executed. If the promise is rejected (meaning something went wrong), the operation inside .catch() will be executed. The catch() will 'catch' the error. You are then able to handle it the way you desire. (Thow error, print to console etc)

Async await

someFunction = async () => {
  try {
    const somePromise = await someAsyncOperation;
    console.log(somePromise);
  } catch (err) {
    console.log(err.message);
  }
};

This example looks more like your actual code. By wrapping the await inside a try...catch block, we are able to do the same as the first example. by using a catch(), the JavaScript compiler has a way to explicitly tell you what went wrong with the operation.

(Note that I wrote the last example wrapped in a async function. This is because using the keyword await is only allowed inside functions that are marked async, whereas a normal .then() chain be used in either.)

To answer your question

You should now be able to figure out why you're receiving the warning:

await message.guild.channels.cache.find(channel => channel.name === "restart").id

If this can't be found, JavaScript would love to tell you the reason why. So in this case, you will have to wrap it in a try..catch block (or use the regular promise syntax instead.)

You can read more about this very cool subject at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise