如何解决discord.js中的“无法读取未定义的属性'react'”

时间:2019-07-09 11:40:03

标签: javascript node.js google-sheets-api discord.js

我正在设置一个带有反应的discord机器人,但是第二反应在编号方面存在错误,但是我找不到它。这可能是循环错误,但是我已经尝试过更改变量的名称,没有任何帮助。对于代码的长度,我也感到抱歉,但是我试图将其缩短到最大。

点击命令!bot后,机器人应以5条可能的反应回复消息。
如果按 reaction 1 ,则会出现第二条消息,其中包含说明,不和和日期。
此消息的底部应显示一个反应✅。
一切正常,无后顾之忧,只不过当我按下此反应时什么也没发生。

client.on('message', async message => {
  if (message.content.toLowerCase().startsWith('!bot')) {

    async function accessSpreadsheet(embed) {
      const doc = new GoogleSpreadsheet('1qA11t460-ceILmwu6RtfiPGb_n9MUD_7z6Ld7I_Z6yc');
      await promisify(doc.useServiceAccountAuth)(creds);
      const info = await promisify(doc.getInfo)();
      var sheet = info.worksheets[0];

      var cells = await promisify(sheet.getCells)({
        'min-row': 2,
        'max-row': 7,
        'min-col': 16,
        'max-col': 16,
        'return-empty': true,
      })
      for (var cell of cells) {

      }
      for (let i = 0; i < 25 && cells[i]; i++) {
        {
          if (i == 0) embed.addField('Name', `•1⃣ ${cells[i].value}`, false);
        } {
          if (i == 1) embed.addField('Name', `•2⃣ ${cells[i].value}`, false);
        } {
          if (i == 2) embed.addField('Name', `•3⃣ ${cells[i].value}`, false);
        } {
          if (i == 3) embed.addField('Name', `•4⃣ ${cells[i].value}`, false);
        } {
          if (i == 4) embed.addField('Name', `•5⃣ ${cells[i].value}`, false);
        }
      }
    }

    var embed = new RichEmbed()
      .setColor('#0099ff')
      .setTitle("A title");

    try {
      var sentMessage = await accessSpreadsheet(embed)
        .then(() => message.author.send(embed))
        .catch(console.error);

      for (let n = 1; n <= 5; n++) await sentMessage.react(`${n}⃣`);

      const filter = (reaction, user) => ['1', '2', '3', '4', '5'].includes(reaction.emoji.name.slice(0, 1)) && user.id === message.author.id;
      const collected = await sentMessage.awaitReactions(filter, {
        maxMatches: 1,
        time: 4000
      });

      if (collected.size === 0) {
        await message.channel.send('Time ran out');
      } else {
        const reaction = collected.first();

        switch (reaction.emoji.name.slice(0, 1)) {
          case '1':
            async function accessSpreadsheet(embed1) {
              const doc = new GoogleSpreadsheet('1qA11t460-ceILmwu6RtfiPGb_n9MUD_7z6Ld7I_Z6yc');
              await promisify(doc.useServiceAccountAuth)(creds);
              const info = await promisify(doc.getInfo)();
              var sheet = info.worksheets[0];

              var cells = await promisify(sheet.getCells)({
                'min-row': 2,
                'max-row': 7,
                'min-col': 16,
                'max-col': 16,
                'return-empty': true,
              })
              for (let i = 0; i < 25 && cells[i]; i++) {
                {
                  if (i == 0) embed1.setTitle(`You select: ${cells[i].value}`);
                }
              }
              var cells1 = await promisify(sheet.getCells)({
                'min-row': 2,
                'max-row': 7,
                'min-col': 17,
                'max-col': 17,
                'return-empty': true,
              })
              for (let j = 0; j < 25 && cells1[j]; j++) {
                {
                  if (j == 0) embed1.setDescription(`Description: \n \n ${cells1[j].value}`);
                }
              }
              var cells1 = await promisify(sheet.getCells)({
                'min-row': 2,
                'max-row': 7,
                'min-col': 18,
                'max-col': 18,
                'return-empty': true,
              })
              for (let l = 0; l < 25 && cells1[l]; l++) {
                {
                  if (l == 0) embed1.addField(`Discord:`, `${cells1[l].value}`, true);
                }
              }
              var cells1 = await promisify(sheet.getCells)({
                'min-row': 2,
                'max-row': 7,
                'min-col': 20,
                'max-col': 20,
                'return-empty': true,
              })
              for (let k = 0; k < 25 && cells1[k]; k++) {
                {
                  if (k == 0) embed1.addField(`Date:`, `${cells1[k].value}`, true);
                }
              }
              for (let k = 0; k < 25 && cells1[k]; k++) {
                {
                  if (k == 0) embed1.addField(`To be validated with ✅:`, `click on reaction`);
                }
              }
            }

            var embed1 = new RichEmbed()
              .setColor('#0099ff');
            await accessSpreadsheet(embed1)
              .then(() => message.author.send(embed1))
              .then(message => {
                message.react('✅')
                //Problem starts here

                client.on('messageReactionAdd', async (reaction, user) => {
                  if (reaction.emoji.name === '✅' && user.id !== client.user.id) {
                    async function accessSpreadsheet(embed2) {
                      var doc = new GoogleSpreadsheet('1qA11t460-ceILmwu6RtfiPGb_n9MUD_7z6Ld7I_Z6yc');
                      await promisify(doc.useServiceAccountAuth)(creds);
                      var info = await promisify(doc.getInfo)();
                      var sheet = info.worksheets[0];
                      var cells = await promisify(sheet.getCells)({
                        'min-row': 2,
                        'max-row': 7,
                        'min-col': 21,
                        'max-col': 21,
                        'return-empty': true,
                      })
                      for (let m = 0; m < 25 && cells[m]; m++) {

                        {
                          if (m == 0) embed2.addField(`Days before toornament: ${cells[m].value}`, `Choose options: \n 1⃣  \n 2⃣ \n 3⃣ \n \n **(5 sec)**`);
                        }
                      }
                    }
                    var embed2 = new RichEmbed()
                      .setColor('#0099ff');
                    try {
                      var sentMessage1 = await accessSpreadsheet(embed2)
                        .then(() => message.author.send(embed2))
                        .catch(console.error);

                      for (let z = 1; z <= 3; z++) await sentMessage1.react(`${z}⃣`); // The error is here

                      const filter = (reaction, user) => ['1', '2', '3'].includes(reaction.emoji.name.slice(0, 1)) && user.id === message.author.id;
                      const collected = await sentMessage1.awaitReactions(filter, {
                        maxMatches: 1,
                        time: 4000
                      });

                      if (collected.size === 0) {
                        await message.channel.send('Time ran out');
                      } else {
                        const reaction = collected.first();

                        switch (reaction.emoji.name.slice(0, 1)) {
                          case '1':
                            message.channel.send('1')
                            break;
                          case '2':
                            await message.channel.send('You chose `two`.');
                            break;
                          case '3':
                            await message.channel.send('You chose `3`.');
                            break;
                        }
                      }
                    } catch (err) {
                      console.error(err);
                    }

                  }
                })
              })

              .catch(console.error);
            break;
          case '2':
            await message.channel.send('You chose `two`.');
            break;
          case '3':
            await message.channel.send('You chose `3`.');
            break;
          case '4':
            await message.channel.send('You chose `4`.');
            break;
          case '5':
            await message.channel.send('You chose `5`.');
            break;
        }
      }
    } catch (err) {
      console.error(err);
    }
  }
})

我知道这仅适用于反应1,但控制台显示:

  

DiscordAPIError :无法向该用户发送消息
   TypeError :无法读取未定义的属性“反应”

在以下行上引发错误:

for (let z = 1; z <= 3; z ++) await sentMessage1.react (`$ {z} ⃣`);

1 个答案:

答案 0 :(得分:0)

我注意到的一件事是您正在使用异步/等待,但是像应许一样对待它。然后

var sentMessage1 = await accessSpreadsheet(embed2)
                        .then(() => message.author.send(embed2))
                        .catch(console.error);

sentMessage1从您的呼叫中未收到任何回馈。

您要么返回上面带有.react的内容,要么处理then语句中的所有内容。

var sentMessage1 = await accessSpreadsheet(embed2)
                        .then(() => {
                             message.author.send(embed2);
                             message.author.react(####); //I don't know the discord API
                         })
                        .catch(console.error);