简体   繁体   中英

Detect Reaction on discord.js

I am making a discord bot, and I have a problem with a waifu command. With this command the bot sends an embed, and I want to detect when someone reacts to this embed. I tried this code but nothing happens.

const Discord = require("discord.js");
const axios = require("axios")
const Client = new Discord.Client;

module.exports = {
    name : 'waifu',
    description : "waifu command",
    execute(message, args) {


        Client.on('messageReactionAdd', (reaction, user) => {
            message.channel.send("Reacted")
        })

        axios.get("https://api.waifu.pics/sfw/waifu")
        .then((res) => {


            let wembed = new Discord.MessageEmbed()
                .setTitle(`Voici votre waifu ${message.author.username}`)
                .setImage(res.data.url)
            message.channel.send(wembed)
            .then(function (message) {
                message.react("❤")
                Client.on('messageReactionAdd', (reaction, user) => {
                    console.log("reacted")
                })

              })


        })
        .catch((err) => {
            console.error("ERR: ", err)
        })

    }
}

A couple things here.

First, you defined message twice; once as the message initiating the command that originated from your command handler, and once as the message the bot sent. You need to rename one of these or else various unexpected issues may arise.

Second, you should never define your client twice. Instead, you should reuse the client. To do this, you can either export it to your command handler the same way you export message and args , or you can access it via <message>.client and use it the same way you did here.

You can't add a messageReactionAdd listener inside your message listener. You can however set up a reaction collector using createReactionCollector for example, like in my example below:

const { MessageEmbed } = require('discord.js');
const axios = require('axios')

module.exports = {
  name: 'waifu',
  description: 'waifu command',
  execute(message, args) {
    axios
      .get('https://api.waifu.pics/sfw/waifu')
      .then((res) => {
        let wembed = new MessageEmbed()
          .setTitle(`Voici votre waifu ${message.author.username}`)
          .setImage(res.data.url);

        // make sure you use a different var name here
        // like msg instead of message
        message.channel.send(wembed).then((msg) => {
          msg.react('❤');

          // set up a filter to only collect reactions with the ❤ emoji
          let filter = (reaction, user) => reaction.emoji.name === '❤';
          let collector = msg.createReactionCollector(filter);

          collector.on('collect', (reaction, user) => {
            // in case you want to do something when someone reacts with ❤
            console.log('reaction added');
          });

          collector.on('remove', (reaction, user) => {
            // in case you want to do something when someone removes their reaction
            console.log('reaction removed');
          });
        });
      })
      .catch((err) => {
        console.error('ERR: ', err);
      });
  },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM