I don't know where exactly I need to post this issue I am having but someone please help me i am losing my mind here.
So I have a small bot for discord, and I am using JIMP for image manipulation. I want to create an image, add things to it, send it to discord channel then delete it. So my code works fine, but sometimes it gives me this error
my/pic/dir is my linux full path to image directory
Error: ENOENT: no such file or directory, stat 'my/pic/dir'
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: 'my/pic/dir' } Promise {
var i1 = Jimp.read(GetUserAvatar);
var i2 = Jimp.read(__dirname + "/images/image1.png");
var Card = __dirname + "/images/" + user.id + ".png";
Promise.all([i1, i2]).then(async images =>
{
images[0].scaleToFit(122, 124).quality(100)
images[1].composite(images[0], 100, 120).quality(100).write(Card));
});
iReloadImageCache5[user.id] = setTimeout(async function ()
{
message.channel.send("", { files: [Card] });
iDeleteCard[user.id] = setTimeout(async function ()
{
if(fs.existsSync(Card))
{
await fs.unlinkSync(Card);
}
}, 5000);
}, 600);
Without knowing where exactly the error occurs in this execution, my guess is that either the writing of the card is happening too late or the deleting is happening too soon for the message.channel.send method.
I suggest removing the need for a temporary file altogether and using buffers to pass to Discord. You may also want to check into seeing if the two source images can be retrieved via buffer.
Writing/reading to file not only takes a lot of time, but it can present some race conditions if you're not constantly doing things asynchronously.
Something like the following. You may need to edit/tweek, as I'm not able to test exactly what you're doing.
var i1 = Jimp.read(GetUserAvatar);
var i2 = Jimp.read(__dirname + "/images/image1.png");
Promise.all([i1, i2]).then(async images =>
{
images[0].scaleToFit(122, 124).quality(100);
images[1].composite(images[0], 100, 120)
.quality(100)
.getBuffer(Jimp.AUTO, (err, buffer) => { // Returns a buffer with an automatic MIME type matching the original file
if(err) { // There was an error, do something about it
console.log('Oh no');
return;
}
message.channel.send("", { files: [buffer] }); // discord.js supports sending the Buffer type as an attachment
});
});
You sir, ARE MY SAVIOUR. I'm literally shaking of happiness. holy fuck. This is exactly what I wanted: no image save, direct edit and send thorough discord. Thanks from all my heart sir! FUCK YES AFTER 5 DAYS OF HEADACHE.
Happy to help!
In the original code, I can tell you it was because you weren't using await/async, which would cause things to start and finish in the wrong order.
Most helpful comment
Without knowing where exactly the error occurs in this execution, my guess is that either the writing of the card is happening too late or the deleting is happening too soon for the
message.channel.sendmethod.I suggest removing the need for a temporary file altogether and using buffers to pass to Discord. You may also want to check into seeing if the two source images can be retrieved via buffer.
Writing/reading to file not only takes a lot of time, but it can present some race conditions if you're not constantly doing things asynchronously.
Something like the following. You may need to edit/tweek, as I'm not able to test exactly what you're doing.