【问题标题】:discord.js putting an image on another image [closed]discord.js将图像放在另一个图像上[关闭]
【发布时间】:2021-10-05 13:48:47
【问题描述】:

我一直在尝试创建一个命令,当您发送图像或图像的 url 时,它会将该图像放在我已经在代码中拥有的另一个图像上,并且每次尝试将其更改为时都会出现错误imageTemp(这是您放置图像的图像)或image`` or messageAttachment```。我会把我的代码放在下面,这样你就可以看到问题所在了。

const { MessageAttachment } = require('discord.js');
const Jimp = require('jimp');
const discord = require('discord.js')

module.exports = {
  name: "img",
  aliases: [],
  run: async (client, message, args) => {
    let messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null
    try {
      let imageTemp = "https://upload.wikimedia.org/wikipedia/commons/8/8a/Banana-Single.jpg"
      let image = await Jimp.read(messageAttachment);
      let buffer = await image.getBufferAsync(Jimp.MIME_JPEG);

    Jimp.read(imageTemp).then(image => {
    image.composite(messageAttachment, 10, 10)
    })

      message.channel.send(new MessageAttachment(buffer));
    } catch (err) {
      console.log(err);
      message.channel.send('Oops, there was an error. Try inputting the command again.');
    }
  },
};

这是我得到的结果和错误https://imgur.com/a/C6RcTOf

【问题讨论】:

  • 你能告诉我们你得到的错误吗?
  • 是的here
  • 顺便说一下我正在使用replit.com
  • @ceqoX 我忘了提到你的用户对不起

标签: javascript node.js discord.js image-manipulation jimp


【解决方案1】:

Jimp.composite() 方法接受 Jimp 图像对象作为其第一个参数。不是您传入的 url (messageAttachment)。此外,您在修改之前将图像保存到缓冲区。

messageAttachmentnull 时,就会出现您提到的错误。如果邮件没有附件,您应该检查并返回。

const imageTemp = "https://upload.wikimedia.org/wikipedia/commons/8/8a/Banana-Single.jpg";
const messageAttachment = message.attachments.size > 0 ? message.attachments.array()[0].url : null;

// Exit if there is no message attachment present in the message
if (!messageAttachment) return;

// Resolve the temporary image url to Jimp object
const firstImage = await Jimp.read(imageTemp);
// Resolve the message attachment image url to Jimp object
const secondImage = await Jimp.read(messageAttachment);

// Composite the two images together (modifies the firstImage object)
firstImage.composite(secondImage, 10, 10);

// Save the resulting image to buffer
const buffer = await firstImage.getBufferAsync(Jimp.MIME_JPEG);

// Send the buffer as a message attachment
message.channel.send("Combined image:", {files: [{ attachment: buffer }]});

【讨论】:

  • 我收到另一个错误,表明它找不到缓冲区 的 MIME。我不确定这意味着什么,因为我做了你展示的事情
  • 你能告诉我整个错误信息以及它发生在哪里吗?
  • 好像this 问题。
  • 这是完整的错误消息imgur.com/a/LzgqeX0
  • 我猜它是用Jimp.read() 在线抛出错误,对吗?更准确地说是messageAttachment。你能做console.log(messageAttachment)吗?并告诉我输出是什么?
猜你喜欢
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多