user18433026
user18433026

Reputation:

catch not working - The promise rejected with the reason "#<Collection>"

I'm trying to write a discord bot, right now it's a command to get married.

Everything works as it should, except for one thing. In the awaitReactions function, I have a time of 10 seconds, and after this time I get this error:

node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); [UnhandledPromiseRejection: This error originated either by throwing inside of a n async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#".] { code: 'ERR_UNHANDLED_REJECTION' }

I can't understand why this is happening, I have .catch() at the end of the function and in theory everything should work as it should.

Why doesn't .catch() work in my case? What could be the problem?

const { Command } = require('discord.js-commando');
const db = require("quick.db");


module.exports = class MarryCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'marry',
      memberName: 'marry',
      group: 'test',
      description: 'Marry the mentioned user',
      guildOnly: true,
      args: [
        {
          key: 'userToMarry',
          prompt: 'Please select the member you wish to marry.',
          type: 'member'
        }
      ]
    });
  }

  run(message, { userToMarry }) {
    const exists = db.get(`${message.author.id}.user`);
    const married = db.get(`${userToMarry.id}.user`);
    if (!userToMarry) {
      return message.channel.send('Please try again with a valid user.')}
    if (exists == message.author.id) {
      return message.channel.send('You are already married!')}
    if (married == userToMarry.id) {
      return message.channel.send('This user is already married!')}
    if (userToMarry.id == message.author.id) {
      return message.channel.send('You cannot marry yourself!');
    }
    if (exists != message.author.id && married != userToMarry.id) {
    message.channel.send(`**Important announcement!**
    
    ${message.author} makes a marriage proposal ${userToMarry}
    
    Are you ready to get married?`).then(message => {
      message.react('👍').then(() => message.react('👎'));
    

      message.awaitReactions((reaction, user) => user.id == userToMarry.id && (reaction.emoji.name == '👍' || reaction.emoji.name == '👎'),
        { max: 1, time: 10000, errors: ['time'] })
      .then(collected => {
        const reaction = collected.first();
        if (reaction.emoji.name === '👎') {
      return message.channel.send('I think **no**...')}
    if (reaction.emoji.name === '👍') {
      db.set(message.author.id, { user: message.author.id, partner: userToMarry.id });
      db.set(userToMarry.id, { user: userToMarry.id, partner: message.author.id });
    message.channel.send(`${message.author} and ${userToMarry} now married!!`)
      .catch(() => {
           message.reply('No reaction after 10 seconds, operation canceled');
        });
      }
  });
});
}}};

Upvotes: 0

Views: 453

Answers (1)

adhi narayan
adhi narayan

Reputation: 320

The mistake here is that every then chain should have a catch block. You have missed two catch blocks. The solution is to either add the catch blocks to all the then chains or you can connect all then chains into one big chain and finally use one catch block

Method 1

const { Command } = require("discord.js-commando");
const db = require("quick.db");

module.exports = class MarryCommand extends Command {
    constructor(client) {
        super(client, {
            name: "marry",
            memberName: "marry",
            group: "test",
            description: "Marry the mentioned user",
            guildOnly: true,
            args: [
                {
                    key: "userToMarry",
                    prompt: "Please select the member you wish to marry.",
                    type: "member",
                },
            ],
        });
    }

    run(message, { userToMarry }) {
        const exists = db.get(`${message.author.id}.user`);
        const married = db.get(`${userToMarry.id}.user`);
        if (!userToMarry) {
            return message.channel.send("Please try again with a valid user.");
        }
        if (exists == message.author.id) {
            return message.channel.send("You are already married!");
        }
        if (married == userToMarry.id) {
            return message.channel.send("This user is already married!");
        }
        if (userToMarry.id == message.author.id) {
            return message.channel.send("You cannot marry yourself!");
        }
        if (exists != message.author.id && married != userToMarry.id) {
            message.channel
                .send(
                    `**Important announcement!**
    
    ${message.author} makes a marriage proposal ${userToMarry}
    
    Are you ready to get married?`
                )
                .then((message) => {
                    message.react("👍")
          .then(() => message.react("👎"))
          .catch(()=>{
            //code
          });
                    message.awaitReactions((reaction, user) =>
                                user.id == userToMarry.id && (reaction.emoji.name == "👍" || reaction.emoji.name == "👎"),
                            { max: 1, time: 10000, errors: ["time"] }
                        ).then((collected) => {
                            const reaction = collected.first();
                            if (reaction.emoji.name === "👎") {
                                return message.channel.send("I think **no**...");
                            }
                            if (reaction.emoji.name === "👍") {
                                db.set(message.author.id, {
                                    user: message.author.id,
                                    partner: userToMarry.id,
                                });
                                db.set(userToMarry.id, {
                                    user: userToMarry.id,
                                    partner: message.author.id,
                                });
                                message.channel
                                    .send(`${message.author} and ${userToMarry} now married!!`)
                                    .catch(() => {
                                        message.reply(
                                            "No reaction after 10 seconds, operation canceled"
                                        );
                                    });
                            }
                        }).catch(()=>{
              //code
            });
                }).catch(()=>{
          //code
        });
        }
    }
};

Method 2

const { Command } = require("discord.js-commando");
const db = require("quick.db");

module.exports = class MarryCommand extends Command {
    constructor(client) {
        super(client, {
            name: "marry",
            memberName: "marry",
            group: "test",
            description: "Marry the mentioned user",
            guildOnly: true,
            args: [
                {
                    key: "userToMarry",
                    prompt: "Please select the member you wish to marry.",
                    type: "member",
                },
            ],
        });
    }

    run(message, { userToMarry }) {
        const exists = db.get(`${message.author.id}.user`);
        const married = db.get(`${userToMarry.id}.user`);
        if (!userToMarry) {
            return message.channel.send("Please try again with a valid user.");
        }
        if (exists == message.author.id) {
            return message.channel.send("You are already married!");
        }
        if (married == userToMarry.id) {
            return message.channel.send("This user is already married!");
        }
        if (userToMarry.id == message.author.id) {
            return message.channel.send("You cannot marry yourself!");
        }
        if (exists != message.author.id && married != userToMarry.id) {
            message.channel
                .send(
                    `**Important announcement!**
    
    ${message.author} makes a marriage proposal ${userToMarry}
    
    Are you ready to get married?`
                )
                .then((message) => {
          
                    message.react("👍")
          .then(() => message.react("👎"))
          .catch(()=>{
            //code
          });
                    return message.awaitReactions((reaction, user) =>
                                user.id == userToMarry.id && (reaction.emoji.name == "👍" || reaction.emoji.name == "👎"),
                            { max: 1, time: 10000, errors: ["time"] }
                        )
                })
        .then((collected) => {
          const reaction = collected.first();
          if (reaction.emoji.name === "👎") {
            return message.channel.send("I think **no**...");
          }
          if (reaction.emoji.name === "👍") {
            db.set(message.author.id, {
              user: message.author.id,
              partner: userToMarry.id,
            });
            db.set(userToMarry.id, {
              user: userToMarry.id,
              partner: message.author.id,
            });
            return message.channel
              .send(`${message.author} and ${userToMarry} now married!!`)
          }
        })
        .catch(()=>{
          message.reply(
            "No reaction after 10 seconds, operation canceled"
          );
        });
        }
    }
};

Upvotes: 0

Related Questions