0

我有反转图像的代码。由于没有关于它的教程,我尝试按照教程来整理命令。

我记录了图像,它显示正常。当我运行命令时,也没有错误,但图像不显示。

module.exports = {
  name: "invert",
  description: "Invert your avatar image",
  async execute(message, member) {
    const Canvas = require("canvas");
    const Discord = require("discord.js");

    let theCanvas = Canvas.createCanvas(218, 218);

    const ctx = theCanvas.getContext("2d");
    const theAvatar = await Canvas.loadImage(message.member.user.displayAvatarURL);
    console.log(theAvatar);
    var imageData = ctx.getImageData(0, 0, theAvatar.width, theAvatar.height);

    for (var i = 0; imageData.data.length; i += 4) {
      imageData.data[i] = 255 - imageData.data[i];
      imageData.data[i + 1] = 255 - imageData.data[i + 1];
      imageData.data[i + 2] = 255 - imageData.data[i + 2];
      imageData.data[i + 3] = 255;
    }

    console.log(imageData);

    const attachment = new Discord.Attachment(imageData.toBuffer());
    message.channel.send(attachment);
  },
};
4

1 回答 1

0

我显示图像的方式要简单得多。

client.on("message", message => {
  if (message.content.startsWith("?" + "avatar")) {
    message.reply(message.author.displayAvatarURL());
  }
});

我认为问题可能出在您所说的const theAvatar = await Canvas.loadImage(message.member.user.displayAvatarURL);

于 2020-05-13T16:48:12.967 回答