2

我正在尝试使用我的机器人创建一个 meme 命令,该命令使用 Jimp 将文本添加到用户使用命令发送的图像上。它可以工作,但是当某些事情没有按计划进行时(例如,有人没有发送图像,有人没有发送需要应用于图像的文本等),它就会出错并使我的机器人崩溃。这是我的代码:

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;

错误:

(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'
}
4

2 回答 2

0

在不知道您要处理的具体错误的情况下,最好的方法是将您的整个代码部分包装在一个try/catch. 在 catch 部分,您可以查看console.warn错误并希望使用日志来调试它并提供更多详细信息。

于 2020-05-17T02:55:05.707 回答
0

return如果用户没有遵循正确的命令,您忘记添加退出逻辑的语句。当用户没有提供参数时,我猜会msg.attachments.first()返回undefined,这就是 Jimp 错误的原因。

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) => {
...
于 2020-05-17T04:28:21.510 回答