2
votes

Is it possible to change an image inside an embed? I'm trying to recreate an "etch-a-sketch" bot I saw on reddit and was wondering how it was done. Here's what I tried so far: This is inside the function that makes the image:

//code that draws the etch-a-sketch
const etchembed = new Discord.MessageEmbed()
    .setAuthor(`${message.author.username}`, `${message.author.displayAvatarURL()}`)
    .setTitle('???? Etch-A-Sketch ????')
    .setColor("#f66868")
    .setFooter(`${client.user.username}`, `${client.user.displayAvatarURL()}`)
    //n is a variable that increases by 1 every time the function is run
    .attachFiles([new Discord.MessageAttachment(canvas.toBuffer(), `etch${n}.png`)])
    .setImage(`attachment://etch${n}.png`)
    .setTimestamp();
return etchembed

In the main command file I do this after awaiting for a return from the function:

message.edit(newetchembed)

All this does is move the image outside of the embed. Am I doing something wrong?

Edit 1:

I tried changing message.edit(...) to message.channel.send(...) and it sends a new embed with the right image just fine. When I try to use message.edit, it just moves the image outside of the embed for some reason.

Edit 2:

I did some more testing and I'm starting to think it's just something wrong with discord or discord.js. This is due to the fact that when I log the file attachments and the image, everything works as it should:

embed 1: [
  MessageAttachment {
    attachment: < Buffer 89 50 4e 47 0 d 0 a 1 a 0 a 00 00 00 0 d 49 48 44 52 00 00 01 94 00 00 01 2 c 08 06 00 00 00 e4 5 c 45 b8 00 00 00 06 62 4 b 47 44 00 ff 00 ff 00 ff a0 bd a7...1167 more bytes > ,
    name: 'etch_1595840597644.png'
  }
] {
  url: 'attachment://etch_1595840597644.png'
}
embed 2: [
  MessageAttachment {
    attachment: < Buffer 89 50 4e 47 0 d 0 a 1 a 0 a 00 00 00 0 d 49 48 44 52 00 00 01 94 00 00 01 2 c 08 06 00 00 00 e4 5 c 45 b8 00 00 00 06 62 4 b 47 44 00 ff 00 ff 00 ff a0 bd a7...1167 more bytes > ,
    name: 'etch_1595840607390.png'
  }
] {
  url: 'attachment://etch_1595840607390.png'
}

As you can see, the message embeds have different image attachments, so I'm not sure why it just moves the original image outside of the embed instead of attaching a new one. This is what it looks like.

Another thing is that it sends the right image when I send a new message instead of editing.

4

4 Answers

2
votes

The attachments of a Message can't be edited - this is a Discord limitation.

You'll have to delete the original Message and send a new one with the new attachment to achieve this.

1
votes

It is not possible to change attachments. But for an image or thumbnail it is possible. just use the function message.edit().

keep in mind that you have to recreate the whole embed you pass into the .edit function!

Link to DiscordJS Documentation

0
votes

Yes, it is possible!

  1. Send a new message containing the new image. (You can send it to a specific channel / server dedicated just for this).

  2. Get the URL of the attached image.

  3. In the Embed you want to edit, change the image url to the url of the new image.

See this answer by Fahad under my similar question.

Alternatively to step 1, you could create a web-server or find an API, which allows you to host the new image yourself. Then you could also edit the Embed's image URL to match that.

-1
votes

Try to change: .attachFiles([new Discord.MessageAttachment(canvas.toBuffer(), `etch${n}.png`)

By: .attachFiles(`etch${n}.png`)

It can solve your issue. Since you don't want to attach a new message but just attach the image to the embed.