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

365
Visualizações
image url showing as undefined discord.js

so I'm making a discord bot as a small project and when I try to get the url from an image attachment it shows up as undefined. here's my code:

var attachment = message.attachments
if (message.content.includes("is dropping 3 cards!")) {
    let image = attachment
    console.log(image.url)
}

when I use this code the image url shows up as undefined and I don't know why. I've tried using image without .url and the url seems to show up but when I use .url it doesn't seem to work.

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

0

Now now, message.attachments contains an array. The attachment can be image, video or file. There can be also multiple attachments in one message. Meaning that you need to define which one of the attachments you want to use. You should also check if there is any attachments in the message before going forward.

Below is little example for using the first file/image in the message:

if(message.attachments.size > 0) {
     var attachment = message.attachments.first();
     console.log(attachment.url)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Try console logging just attachment and seeing if that gives you any results. It should return an array of attachments which you would either need to map through or set an index value to your attachment ie. attachment[0].url. Also, there's no need for declare the image variable as its not saving you any time. attachment.url is the same thing as image.url and it just makes things more confusing than it needs to be.

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