user18433026
user18433026

Reputation:

Reply to a suggestion with a reaction

I'm trying to create a bot for a discord server. I added the "marry" command to it.

When a user makes an offer, an announcement message appears. I've added two reactions to this post.

Now you can answer the offer by writing yes or no.

But I want to make it possible to answer the offer by clicking on the reaction, the first reaction is yes, the second is no. Will it be hard to do?

I did everything as in the documentation https://discordjs.guide/popular-topics/reactions.html#awaiting-reactions

But my bot does not react in any way to clicking reactions, please help..

(`Are you ready to get married?`).then(message => {
      message.react("👍")
      message.react("👎")
    });

const filter = (reaction) => {
      return ['👍', '👎'].includes(reaction.emoji.name) && message.author.id === userToMarry.id;
    };

      message.awaitReactions({ filter, max: 1, time: 60000, 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(err => {
        message.channel.send(
          `Something went wrong while trying to marry this user. ${err}`
        );
        return console.log(err)});
      }
  })
  .catch(collected => {
        message.reply('You reacted with neither a thumbs up, nor a thumbs down.');
    });

Here is the complete file:

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("👍")
      message.react("👎")
    });
      message.channel.awaitMessages(message => message.author.id == userToMarry.id, {max: 1}).then(collected => {
    if (collected.first().content.toLowerCase() == 'no') {
      return message.channel.send('I think **no**...')}
    if (collected.first().content.toLowerCase() == 'yes') {
      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(err => {
        message.channel.send(
          `Something went wrong while trying to marry this user. ${err}`
        );
        return console.log(err)});
      }
  });
}}};

Upvotes: 2

Views: 215

Answers (1)

user18342812
user18342812

Reputation:

Try like this

.then(msg=> {
      msg.react('👍').then(() => msg.react('👎'));
    

      msg.awaitReactions((reaction, user) => user.id == userToMarry.id && (reaction.emoji.name == '👍' || reaction.emoji.name == '👎'),
        { max: 1, time: 60000, 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!!`)
 
    ...

  });
});

Upvotes: 1

Related Questions