Swerk
Swerk

Reputation: 33

User image to Discord Embed

I want the image that a user sends to be fetched and put into an embed that the bot will then send. As the image must be a url and the person sends an image, I don't know how I can transform this image into a url. When doing the command, the embed send but without the image. This is what i've tried but it didn't work :

const plotEmbed = new Discord.MessageEmbed() 
                        .setTitle(`Purchase of the plot n°${plotId}`)
                        .setColor("GREEN")
                        .setDescription(`Purchase of plot ${plotId} for ${days} days.`)
                        .setImage(message.attachments.first.url)
                        .setTimestamp()
message.channel.send({ embeds: [pEmbed]})

If someone could get me an advice on what I should try because I don't really know... Thanks for reading.

Upvotes: 0

Views: 992

Answers (1)

Caladan
Caladan

Reputation: 2337

Using message.attachments gives a collection, so you can use the .map() command to get the urls. All you have to do is change your code to:

const attachmentURL = message.attachments.map(attachment => attachment.url)
const plotEmbed = new Discord.MessageEmbed()
  .setTitle(`Purchase of the plot n°${plotId}`)
  .setColor("GREEN")
  .setDescription(`Purchase of plot ${plotId} for ${days} days.`)
  .setImage(attachmentURL[0])
  .setTimestamp()
message.channel.send({
  embeds: [pEmbed]
})

This might just be the fact that pEmbed was declared before your code snippet but just wanted to tell that you are sending an embed called pEmbed while the embed in your current code snippet is in a variable called plotEmbed, so you might want to change that.

Edit:

Just found another way which is the simpler one. You just made a mistake calling message.attachments.first. It should actually be message.attachments.first().url because first() is actually a function. So this should also work exactly like the first method I posted

Upvotes: 1

Related Questions