Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

544
Visualizações
Discord.js: Can fetch a message by ID, but breaks when I replace the ID with a variable

I'm trying to dynamically check for reactions on the message that was replied to, and it works fine when I paste the literal message id, but when I assign it to a variable, it gives a TypeError: Cannot read properties of null (reading 'users').

I used msg.fetchReference() to get the reference message, and it's assigned to repliedTo. I've confirmed that repliedTo.id gives the id that worked when I pasted it in, and that the variable returns a string.

cacheChannel.messages.fetch(repliedTo.id).then(reactionMessage => {
    reactionMessage.reactions.resolve('📌').users.fetch().then(userList => {
        userList.map((user) => {
            if (user.id === 'testID') {
                console.log('already reacted');
            }
        });
    });
});

Side note: I originally tried this without trying to fetch the message as it is now, and just using repliedTo instead of reactionMessage, but I had the same issue.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your code you are using === which also checks if x1 and x2 have the same data types! As you said, variable returns you a String, when user.id is a Number, so I'd recommend to try changing === to ==, which only checks value! Also here is an explanation how it works!

about 4 years ago · Juan Pablo Isaza Relatório

0

So the problem is, when the ID mixed with letters it gives an error? You can use isNaN function to return it if the Id doesn't have any numbers or the id got mixed with letters.

cacheChannel.messages.fetch(repliedTo.id).then(reactionMessage => {
    reactionMessage.reactions.resolve('📌').users.fetch().then(userList => {
        userList.map((user) => {
            if(isNaN(user.id)) {
                return message.reply("Your_Message")
            } else if (user.id === 'testID') {
                console.log('already reacted');
            }
        });
    });
});

Or you can use try {} catch {} function.

cacheChannel.messages.fetch(repliedTo.id).then(reactionMessage => {
    reactionMessage.reactions.resolve('📌').users.fetch().then(userList => {
        userList.map((user) => {
            try {
              if (user.id === 'testID') {
                console.log('already reacted');
              }  
            } catch {
              return message.reply("Your_Message")
            } 
        });
    });
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda