Reputation: 59
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.
Upvotes: 0
Views: 327
Reputation: 21
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.
Upvotes: 0
Reputation: 940
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)
}
Upvotes: 1