简体   繁体   中英

Jimp error: No matching constructor overloading was found

I'm trying to create a meme command with my bot that uses Jimp to add text onto an image the user sends with the command. It works, but whenever something doesn't go to plan (eg someone not sending the image, someone not sending the text that needs to be applied to the image, etc.) it gets an error and crashes my bot. Here's my code:

case "meme":
const [topText, bottomText] = args.slice(1).join(" ").split(",");
msg.channel.startTyping();
if (!args[1]) msg.channel.send("You need to give the text you want to apply to the image!");
Jimp.read(msg.attachments.first(), (err, lenna) => {
  Jimp.loadFont(Jimp.FONT_SANS_128_WHITE).then(font => {
    if (err) console.log(err);
    lenna
      .resize(1280, 1080)
      .quality(100) // set quality
      .print(font, 75, 20, {
        text: topText,
        alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
        alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
      }, 1100)
      .print(font, 75, 900, {
        text: bottomText,
        alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
        alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
      }, 1100)
      .write("./tmp/" + msg.author.id + ".jpg"); // save
  });
});
for (i = 0; i < (1); i++) {
  setTimeout(function () {
    msg.channel.send({
      files: ["./tmp/" + msg.author.id + ".jpg"]
    })
    msg.channel.stopTyping();
    for (i = 0; i < (1); i++) {
      setTimeout(function () {
        fs.unlinkSync("./tmp/" + msg.author.id + ".jpg")
      }, 3 * 1000)
    }
  }, 3 * 1000)
}
break;

Error:

(node:32440) UnhandledPromiseRejectionWarning: Error: No matching constructor overloading was found. Please see the docs for how to call the Jimp constructor.
    at Jimp.throwError (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\utils\dist\index.js:35:13)
    at new Jimp (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\core\dist\index.js:502:85)
    at _construct (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@babel\runtime\helpers\construct.js:19:21)
    at C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\core\dist\index.js:1016:32
    at new Promise (<anonymous>)
    at Function.Jimp.read (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\core\dist\index.js:1015:10)
    at Client.<anonymous> (C:\Users\lqshkiwi\Desktop\Discord Bot\index.js:53:18)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
(node:32440) 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:32440) [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.
(node:32440) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat 'C:\Users\lqshkiwi\Desktop\Discord Bot\tmp\652940695585292299.jpg'
(node:32440) 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: 2)
internal/fs/utils.js:230
    throw err;
    ^

Error: ENOENT: no such file or directory, unlink './tmp/652940695585292299.jpg'
    at Object.unlinkSync (fs.js:1053:3)
    at Timeout._onTimeout (C:\Users\lqshkiwi\Desktop\Discord Bot\index.js:80:32)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7) {
  errno: -4058,
  syscall: 'unlink',
  code: 'ENOENT',
  path: './tmp/652940695585292299.jpg'
}

Without know what specific error you're trying to handle against, the best way is to wrap your entire section of code in a try/catch . In the catch portion, you can console.warn the error and hopefully use the logs to debug this with more details.

You forgot to add a return statement to exit the logic if the user didn't follow the proper commands. When the user didn't provide the argument, I'm guessing msg.attachments.first() returns undefined which is why Jimp errors.

if (!args[1]) {
  return msg.channel.send("You need to give the text you want to apply to the image!");
} 

try { // it's good to have a try catch when dealing with asynchronous code
  Jimp.read(msg.attachments.first(), (err, lenna) => {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM