NodeJS/DiscordJS UnhandledPromiseRejectionWarning Error

845 Views Asked by At

First of all, I'm pretty new to programming. Apologies if this post sounds naive.

I'm making a Discord bot using JS, and using a command and event handler instead of everything in main.js. The error occurs when I issue the command !reactionrole.

Here is the error:

(node:4) 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)

here is my code in main.js:

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const fs = require('fs');

client.commands = new Discord.Collection();
client.events = new Discord.Collection();


['command_handler', 'event_handler'].forEach(handler =>{
    require(`./handlers/${handler}`)(client, Discord);
})


client.login(process.env.token);

Here is my code in ready.js

module.exports = () => {
    console.log('The bot is online.')
}

Here is my code in reactionrole.js (a command) just in case it's needed.

module.exports = {
    name: 'reactionrole',
    description: "Sets up reaction roles",
    async execute(message, args, Discord, client) {
        const channel = '796928981047705602';
        const uploadNotifs = message.guild.roles.cache.find(role => role.name === "Upload Notifs");
        const liveNotifs = message.guild.role.cache.find(role => role.name === "Live Notifs");
        const giveNotifs = message.guild.role.cache.find(role => role.name === "Giveaways");

        const uploadNotifsEmoji = ':bell:';
        const liveNotifsEmoji = ':red_circle:';
        const giveNotifsEmoji = ':partying_face:';

        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Choose what to be notified for!')
            .setDescription('Select the types of notifications you want to recieve.\n\n'
                + `${uploadNotifsEmoji} for Upload Notifications`
                + `${liveNotifsEmoji} for Livestream Notifications`
                + `${giveNotifsEmoji} for Giveaway Notifications`);

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(uploadNotifsEmoji);
        messageEmbed.react(liveNotifsEmoji);
        messageEmbed.react(giveNotifsEmoji);
    }
}

Thanks in advance

3

There are 3 best solutions below

0
On BEST ANSWER

Fixed. I didn't have my params set in the correct order. Before, I had async execute(message, args, Discord, client), I changed it to execute(client, message, args, Discord). worked fine after that.

2
On

You're getting this error because you can't simply react with strings such as ':bell:'. Instead, you have to provide the literal unicode value ('')

const uploadNotifsEmoji = '';
const liveNotifsEmoji = '';
const giveNotifsEmoji = '';
2
On

Putting it tiny bit oversimplified - your asynchronous function throws an error in your await statement and it has nowhere to go so it errors out. Maybe the channel does not respond or you cant connect.

You don't know since you have no error handler. A Promise is a wrapper for values so that we can deal with asynchronous workflows in a synchronous way.

See for example this; Nodejs documentation or this; Promises explained

I have no way of testing it right now but from the top of my head you could try to put your call to discord in a try/catch block. This will show you the error in console to make it easier to find, as so;

try {
  let messageEmbed = await message.channel.send(embed);
  messageEmbed.react(uploadNotifsEmoji);
  messageEmbed.react(liveNotifsEmoji);
  messageEmbed.react(giveNotifsEmoji);
}
  catch(error) {
  console.log(error);
}

Here is another good post on the topic try/catch blocks with async/await