首页 > 解决方案 > 现在无法获得对特定消息的识别反应

问题描述

在以前的帖子中,我在让机器人识别反应时遇到问题,但修复工作有效,然后将其更改为对机器人随后说的消息做出反应,现在它不再工作,我尝试更改用户条件,所以它的原始命令作者,但这似乎不起作用

因此,您运行代码,它使嵌入完美并对其做出反应,但是它无法识别您何时做出反应并发出超时消息


exports.run = async (client, message, args) => { 
  
  message.delete()
  
  const MINUTES = 5;
  const questions = [
      { answer: null, field: 'placeholdquestion' },
      { answer: null, field: 'placeholdquestion' },
      { answer: null, field: 'placeholdquestion' },
    ]; //to add more questions just add another line of the above code {answes: null, field: `Question Here`}
    let current = 0;

const commanduser = message.author.id
  // ...

    // wait for the message to be sent and grab the returned message
    // so we can add the message collector
    const sent = await message.author.send(
      `${questions[current].field}`,
    );

    const filter = (response) => response.author.id === message.author.id;
    // send in the DM channel where the original question was sent
    const collector = sent.channel.createMessageCollector(filter, {
      max: questions.length,
      time: MINUTES * 60 * 1000,
    });

    // fires every time a message is collected
    collector.on('collect', (message) => {
//if (questions > 1 && questions < 10) {
      
      // add the answer and increase the current index HERE
      questions[current++].answer = message.content;
      const hasMoreQuestions = current < questions.length;  //change to be an imput of how many questions you want asked

      if (hasMoreQuestions) {
        message.author.send(
          `${questions[current].field}`,
        );
      }
    });

    // fires when either times out or when reached the limit
    collector.on('end', (collected, reason) => {
      if (reason === 'time') {
        return message.author.send(
          `I'm not saying you're slow but you only answered ${collected.size} questions out of ${questions.length} in ${MINUTES} minutes. I gave up.`,
        );
      }


      
      const embed = new MessageEmbed()
        .setTitle("LOA Request")
      
          .addFields(
                { name: 'placehold', value: questions[0].answer+'/10' },
                { name: 'placehold', value: questions[1].answer+'/10' },
                { name: 'placehold', value: questions[2].answer+'/10', inline: true },)
        .setColor(`#1773BA`)
        .setTimestamp()
        .setThumbnail("https://media.discordapp.net/attachments/772915309714735205/795378037813805126/mvg_clean_2.png")
        .setFooter("request by: " + message.author.tag);
        ;
            
  
message.channel.send(embed)
            .then(function (message) {
              message.react("")
              message.react("")})
      
const filter = (reaction, user) => {
    return ['', ''].includes(reaction.emoji.name) && user.id === commanduser; //changed to try and fix it didnt work as message.author.id or this
};

message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] } ) 
    .then(collected => {
        const reaction = collected.first();

        if (reaction.emoji.name === '') {
            message.channel.send('you reacted with a thumbs up.');
        }
        else {
            message.reply('you reacted with a thumbs down.');
        }
    })
    .catch(collected => {
        console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
     message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
    });
      
   
  });
 
  
      ;
}

标签: discord.js

解决方案


你有一个轻微的逻辑错误。您需要将代码从您的第二个filtermessage.awaitReactions您的message.channel.send(embed).then(function (message)...)方法内部。在您的代码中,机器人正在尝试检查message您已经删除的原始 的反应(因为awaitReactions超出了您发送和响应嵌入的函数的范围)。

像这样:

message.channel.send(embed)
    .then(function (message) {
        message.react("")
        message.react("")

        const filter2 = (reaction, user) => {
            return ['', ''].includes(reaction.emoji.name) && user.id === commanduser;
        };

        message.awaitReactions(filter2, { max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
                const reaction = collected.first();

                if (reaction.emoji.name === '') {
                    message.channel.send('you reacted with a thumbs up.');
                }
                else {
                    message.reply('you reacted with a thumbs down.');
                }
            })
            .catch(collected => {
                console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
                message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
            });
    })

推荐阅读