1
votes
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
Hi and welcome to StackOverflow, find some information here on how to ask a good question: stackoverflow.com/help/how-to-ask. Please also format code blocks with the backticks (see the text editor). And finally, what is your quesatoins and what have you tried to solve it?Clara
exports.run returns a promise. If the caller doesn't handle its rejection, or the await operator is not in a try/catch block, then failing to find a channel named restart will produce the error. Now, what's the question?traktor

1 Answers

1
votes

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