#UnhandledPromiseRejection This error originated either by throwing inside of an async function

1 messages Β· Page 1 of 1 (latest)

modern yarrow
#

I've tried my best to comment what I believe my code is doing (or rather what it's supposed to be doing).

Code in separate message, please hold...

#
const { SlashCommandBuilder } = require('@discordjs/builders')

module.exports = {
    data: new SlashCommandBuilder()
        .setName('vote')
        .setDescription('(For Chip Only!) Make a poll!')
        .addStringOption(option =>
            option.setName('question')
                .setDescription('The question to be asked')
                .setRequired(true)),
    async execute(interaction, client, MessageEmbed, minutes) {
        // Grab the question from the interaction options
        const question = interaction.options.getString('question')
        // Define poll embed
        let poll = new MessageEmbed()
            .setTitle(`Poll: ${question}`)
            .setDescription('React with the appropriate emoji to vote!')
            .setFooter({ text: 'Wee6, a bot by ChunkleChip', iconURL: `https://cdn.discordapp.com/avatars/${client.user.id}/${client.user.avatar}.png?size=4096` })
        // Define the reply to the interaction, which is the poll question itself
        poll = await interaction.reply({ embeds: [poll], fetchReply: true })
        // Add reactions and consequently send reply at the same time
        poll.react('πŸ‘').then(() => poll.react('πŸ‘Ž')).then(() => poll.react('πŸ€·β€β™‚οΈ'))
        // Define a filter for the reactions once they've been collected
        const filter = (reaction, user) => {
            return (['πŸ‘', 'πŸ‘Ž', 'πŸ€·β€β™‚οΈ'].includes(reaction.emoji.name) && user.id === message.author.id)
        }
        // Wait 10 seconds for reactions to be collected, then map the reactions for parsing as a edit to the original embed
        poll.awaitReactions({ filter, max: 65535, time: 10000, errors: ['time'] })
            .then(collected => {
                const results = collected.map(reaction => reaction.count)
#
                const totalVotes = results.reduce((a, b) => a + b, 0)
                const resultsString = results.map(result => `${result} ${result.emoji.name}`).join(', ')
                interaction.editReply({ content: `Results: ${resultsString}. Total: ${totalVotes}`, ephemeral: false })
            })
    },
}
#

I will admit, I don't fully understand promises, but I am trying my best.

jagged sparrow
#

so what is the full error of the code ?

modern yarrow
#

Oh, I guess I should ask a question. What am I doing wrong here? This is the full error.

3/31/2022, 11:38:43 AM - Bot is online, Logged in as 'Wee6-V3' 
-- BOT MESSAGE:  --
node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: 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(). The promise rejected with the reason "#<r>".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

Node.js v17.8.0
jagged sparrow
#

the error is not really clear which cause it tho sad

modern yarrow
#

That's my point exactly. It throws this error after the embed gets sent then has reactions put on it.

#

After the 10 second timeout

jagged sparrow
#

you can try to try catch it to see errors

#
try {
// block of code
} catch (error) {
console.error(error)
}
modern yarrow
#

Alright, lemme take a crack at it.

jagged sparrow
modern yarrow
#
const { SlashCommandBuilder } = require('@discordjs/builders')

module.exports = {
    data: new SlashCommandBuilder()
        .setName('vote')
        .setDescription('(For Chip Only!) Make a poll!')
        .addStringOption(option =>
            option.setName('question')
                .setDescription('The question to be asked')
                .setRequired(true)),
    async execute(interaction, client, MessageEmbed, minutes) {
        const question = interaction.options.getString('question')
        let poll = new MessageEmbed()
            .setTitle(`Poll: ${question}`)
            .setDescription('React with the appropriate emoji to vote!')
            .setFooter({ text: 'Wee6, a bot by ChunkleChip', iconURL: `https://cdn.discordapp.com/avatars/${client.user.id}/${client.user.avatar}.png?size=4096` })
        interaction.reply({ embeds: [poll], fetchReply: true }).then(async (message) => {
            await message.react('πŸ‘')
            await message.react('πŸ‘Ž')
            message.awaitReactions({ max: 10, time: 10000, errors: ['time'] })
                .then(collected => {console.log(collected.size)})
                .catch(collected => {
                    const upDoot = collected.get('πŸ‘').count - 1
                    const downDoot = collected.get('πŸ‘Ž').count - 1
#
                    const embed = new MessageEmbed()
                        .setTitle(`Poll Results: ${question}`)
                        .setDescription(`${upDoot} people voted for πŸ‘\n\n${downDoot} people voted for πŸ‘Ž`)
                        .setFooter({ text: 'Wee6, a bot by ChunkleChip', iconURL: `https://cdn.discordapp.com/avatars/${client.user.id}/${client.user.avatar}.png?size=4096` })
                    message.edit({ embeds: [embed] })
                })
        })
    },
}

Update for how I fixed it πŸ™‚