简体   繁体   English

检测 discord.js 上的反应

[英]Detect Reaction on discord.js

I am making a discord bot, and I have a problem with a waifu command.我正在制作一个 discord 机器人,但我对 waifu 命令有疑问。 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;首先,您定义了两次message 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.为此,您可以像导出messageargs一样将它导出到命令处理程序,或者您可以通过<message>.client访问它并像此处一样使用它。

You can't add a messageReactionAdd listener inside your message listener.您不能在message侦听器中添加messageReactionAdd侦听器。 You can however set up a reaction collector using createReactionCollector for example, like in my example below:但是,您可以使用createReactionCollector设置反应收集器,例如下面的示例:

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);
      });
  },
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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