0
votes

I am trying to check the reactions of a specific message by storing its message id. I'm using discord.js.

The problem

The problem is that when using my code, I get this error: TypeError: Cannot read property 'id' of undefined. Maybe it's because it's an embed? What I am trying to do is getting the id of the sent embed and writing it into a file. Then, in the code that handles the reactions, I should get the id wrote on the file, and test if that is the correct message the reaction has been added to.

My code

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}`);
  }
1

1 Answers

1
votes

In your code, you are doing:

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

That's the wrong way to do it. async/await is essentially an easier way of using Promises; in essence, it is a replacement for .then(). So that above code is the equivalent of doing message.channel.send().then().then(), meaning sent is either now still a Promise (with a .then() method) or simply undefined instead of the message object you want it to be.

The simple solution to this is to stop trying to use two different methods of handling Promises at the same time. Use either async/await or .then() but don't use both at once. async/await is much easier to use, so we'll go with that in this example solution.

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}`);

}

I additionally removed a few unnecessary redundancies from your provided code (both in your duplicate checks for the dittatore role and your defining of both a sent and embedMessage variable when both are supposed to be the same message).