投票重新发布Discord.js中的消息

问题描述

我一般对编程和discord.js还是陌生的,所以我使用了指南中的命令处理程序。我正在尝试创建一个机器人,当您执行!spin时,它会从我制作的gameList数组中为您提供游戏。可以按预期工作,并以丰富的嵌入形式发送出去。

我想做到这一点,以便如果小组对该游戏不满意,他们可以以至少3票的优势重新投票。为此,我尝试使用对消息的反应进行尝试,但无法真正起作用

module.exports = {
  name: 'spin',description: 'Spins the wheel!',execute(message) {
    const gameList = ['Games inside array'];
    var x = Math.floor(Math.random() * gameList.length);
    var games = gameList[x];

    const VoteEmbed = new discord.MessageEmbed()
      .setColor('#F8AA2A')
      .setTitle('?Game Spinner?')
      .addField(games,' was the chosen game!');

    message.channel.send(VoteEmbed).then((VoteEmbed) => {
      VoteEmbed.react('?');
    });

    var noCount = 0;

    const filter = (reaction,user) => {
      return [`?`].includes(reaction.emoji.name);
    };

    const collector = message.createReactionCollector(filter,{ time: 10000 });
    collector.on('collect',(reaction,reactionCollector) => {
      if (reaction.emoji.name === `?`) {
        noCount += 1;
      }
    });

    collector.on('end',reactionCollector) => {
      if (noCount >= 3) {
        message.channel.send(VoteEmbed).then((VoteEmbed) => {
          VoteEmbed.react('?');
        });
      }
    });
  },};

没有错误出现,只有经过三票投票,它才不会发送新的VoteEmbed。抱歉,这是一个愚蠢的问题。

解决方法

您的错误位于此处:const collector = message.createReactionCollector(filter,{time: 10000});

当您要收听对已发送消息的反应时,您将在最后收到的消息上创建一个反应收集器。

您需要使用以下代码替换代码:

主要编辑是更改responseCollector以收听voteEmbed消息^

const Discord = require('discord.js');


module.exports = {
  name: "spin",description: "Spins the wheel!",execute(message) {
    const gameList = [Games inside array]
        var x = Math.floor(Math.random() * gameList.length);
        var games = gameList[x];

        const voteEmbed = new Discord.MessageEmbed()
        .setColor("#F8AA2A")
        .setTitle("?Game Spinner?")
        .addField(games," was the chosen game!");


        message.channel.send(voteEmbed).then(voteEmbed => {
            voteEmbed.react('?')

            var noCount = 0;
            
            const filter = (reaction,user) => {
                return [`?`].includes(reaction.emoji.name);

            const collector = voteEmbed.createReactionCollector(filter,{time: 10000});
            collector.on('collect',(reaction,reactionCollector) => {
                if (reaction.emoji.name === `?`) {
                    noCount+=1
                }
            });

            collector.on('end',reactionCollector) => {
                if (noCount >= 3){
                    message.channel.send(voteEmbed).then(voteEmbed => {
                        voteEmbed.react('?')
                    })
                 }
            });
        })
      };
    }
};