Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord.js sending base64 encoded image to channel

i've been at this for days now and im stuck on this final part. as the title suggests my creation receives a base64 encoded image. it then loads it into a buffer and attempts to send it to to a channel like so:

sfbuffer = new Buffer.from(base64_img, "base64");
const finalattach = new Discord.MessageAttachment(sfbuffer);
message.channel.send(finalattach);

it does send the buffer however it always results in this

example of base64 data that gets loaded

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAEA and so on...

i've tried sending it with file options (didn't even send), and as an embed using both .setimage and .attachfiles and both produce the same results. please im banging my head over this. all the google links are purple and i don't know what else to do D:

like image 217
Lexy Avatar asked Nov 20 '25 23:11

Lexy


1 Answers

image buffers should NOT contain the data:image/png;base64, part. only the non-human readable data

split the base64 string at the comma, load it into a attachment as a buffer and send attachment e.g.

const sfbuff = new Buffer.from(base64_img.split(",")[1], "base64");
const sfattach = new Discord.MessageAttachment(sfbuff, "output.png");
message.channel.send(sfattach)

Even though i figured it out, i couldn't of done it without Aviv lo Thank you all :)

like image 156
Lexy Avatar answered Nov 22 '25 15:11

Lexy