簡體   English   中英

如何解決discord.js中的“無法讀取未定義的屬性'反應'”

[英]How to fix “Cannot read property 'react' of undefined” in discord.js

我正在建立一個帶有反應的discord機器人,但是第二反應在編號上有錯誤,但是我找不到它。 這可能是循環錯誤,但是我已經嘗試過更改變量的名稱,沒有任何幫助。 對於代碼的長度,我也感到抱歉,但是我嘗試將其縮短到最大。

擊中命令!bot ,該bot會以一條帶有5種可能反應的消息答復。
如果按反應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 :無法讀取未定義的屬性“ react”

錯誤在以下行中引發:

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

我注意到的一件事是,您正在使用異步/等待,但像應許一樣處理它。

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

sentMessage1不會從您的電話收到任何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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM