【问题标题】:Check reactions of a specific message on Discord.js?检查 Discord.js 上特定消息的反应?
【发布时间】:2021-04-15 08:14:51
【问题描述】:

我正在尝试通过存储消息 ID 来检查特定消息的反应。我正在使用 discord.js。

问题

问题是,在使用我的代码时,我收到此错误:TypeError: Cannot read property 'id' of undefined。也许是因为它是一个嵌入?我想要做的是获取已发送嵌入的 id 并将其写入文件。然后,在处理反应的代码中,我应该得到写在文件上的 id,并测试这是否是添加反应的正确消息。

我的代码

async function tembed(message) {
  
    const mention = message.mentions.members.first();
        if (mention.roles.cache.find(r => r.name === "dittatore")) {
          message.channel.send(`Cannot eject **${mention}**.`);
          message.channel.bulkDelete(1);
        }
        if (mention.roles.cache.find(r => r.name === "dittatore")) return;
        message.channel.bulkDelete(1);
        const testembed = {
          color: '#7A2F8F',
          title: 'test',
          description: `${mention} this is a test embed`,
          footer: 'test footer',
        };
        let sent = await message.channel.send({ embed: testembed }).then(async (embedMessage) => {
          await embedMessage.react('✅');
          await embedMessage.react('❌');
        });
        let id = sent.id; //TypeError: Cannot read property 'id' of undefined
        fs.writeFile('/home/pi/botstuff/testembed.txt', id, (err) => { 
          if (err) throw err;
        });
        console.log(`wrote ${id} on file`);
        const channel = message.guild.channels.cache.get("770735002182877224");
        channel.send(`${mention}`);
  }

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    在你的代码中,你正在做:

    let sent = await message.channel.send({ embed: testembed }).then()
    

    这是错误的做法。 async/await 本质上是使用 Promises 的一种更简单的方式;本质上,它是.then()替换。所以上面的代码相当于做message.channel.send().then().then(),这意味着sent现在仍然是Promise(使用.then()方法)或者只是undefined而不是你想要的message对象.

    解决这个问题的简单方法是停止尝试同时使用两种不同的方法来处理 Promise。使用async/await.then(),但不要同时使用两者。 async/await 更容易使用,因此我们将在此示例解决方案中使用它。

    async function tembed(message) {
        const mention = message.mentions.members.first();
    
        if (mention.roles.cache.find(r => r.name === "dittatore")) {
            message.channel.send(`Cannot eject **${mention}**.`);
            message.channel.bulkDelete(1);
            return;
        }
    
        message.channel.bulkDelete(1);
    
        const testembed = {
          color: '#7A2F8F',
          title: 'test',
          description: `${mention} this is a test embed`,
          footer: 'test footer',
        };
    
        let sent = await message.channel.send({ embed: testembed });        
        await sent.react('✅');
        await sent.react('❌');
        let id = sent.id; //TypeError no longer occurs
        
        fs.writeFile('/home/pi/botstuff/testembed.txt', id, (err) => { 
          if (err) throw err;
        });
        console.log(`wrote ${id} on file`);
        const channel = message.guild.channels.cache.get("770735002182877224");
        channel.send(`${mention}`);
    
    }
    

    我还从您提供的代码中删除了一些不必要的冗余(在您对dittatore 角色的重复检查以及您对sentembedMessage 变量的定义中,两者都应该是相同的消息) .

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 2021-05-18
      • 2021-11-13
      • 2021-03-31
      • 2021-03-23
      • 2020-09-07
      • 2020-02-21
      • 2021-05-13
      • 2020-06-25
      相关资源
      最近更新 更多