我有反转图像的代码。由于没有关于它的教程,我尝试按照教程来整理命令。
我记录了图像,它显示正常。当我运行命令时,也没有错误,但图像不显示。
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);
},
};