lukechilds / merge-images

Easily compose images together without messing around with canvas
MIT License
1.68k stars 163 forks source link

I'm using merge-images in a discord bot so it is throwing an error on node js #95

Open Frisk17 opened 4 years ago

Frisk17 commented 4 years ago
UnhandledPromiseRejectionWarning: Error: Couldn't load image
    at img.onerror (~/Desktop/Discord/node_modules/merge-images/dist/index.umd.js:39:46)
    at setSource (~/Desktop/Discord/node_modules/canvas/lib/image.js:91:13)
    at ~/Desktop/Discord/node_modules/canvas/lib/image.js:59:11
    at ~/Desktop/Discord/node_modules/simple-get/index.js:89:7
    at IncomingMessage.<anonymous> (/Users/mac-guy/Desktop/Discord/node_modules/simple-concat/index.js:7:13)
    at Object.onceWrapper (events.js:421:28)
    at IncomingMessage.emit (events.js:327:22)
    at IncomingMessage.EventEmitter.emit (domain.js:485:12)
    at endReadableNT (_stream_readable.js:1224:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:70029) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:70029) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I have installed Canvas node module but I'm still getting this error. Here's the code:

const mergeImages = require('merge-images');
const { Canvas, Image } = require('canvas');
const { Client, MessageAttachment } = require('discord.js');
const Bot = new Client();

const BOT_TOKEN = "token";

Bot.login(BOT_TOKEN);

Bot.on(
    'ready', () => 
    {
        console.log("Bot is now online!");
    }
);

Bot.on(
    'message', message =>
    {
        if (message.content == "!avatar")
        {
            var url;

            mergeImages(['https://imgur.com/GGo99Gz.png', message.author.displayAvatarURL()], {
                Canvas: Canvas,
                Image: Image
              }).then(b64 => url = b64);

            message.reply(url);
        }
    }
);

Bot.login(BOT_TOKEN);

I actually want to send the image but that's irrelevant to this issue so I just tested by sending the message and it's throwing that error before it so what could be the issue?

lukechilds commented 4 years ago

Can you try downloading the images first and then passing the file paths into merge-images like in the readme example:

https://github.com/lukechilds/merge-images#nodejs-usage

However it looks like node-canvas now supports loading images via URLs (https://github.com/Automattic/node-canvas#imagesrc) so this should work. I don't have time to look into this right now but I'll accept a PR if you can resolve the issue.

For now a quick solution would probably be for you to just download the images before passing to merge-images.