inverts an image attached to the prefix command
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN}); //import standard lib
const { createCanvas, loadImage } = require("canvas"); // import canvas
const fetch = require('node-fetch-npm'); // import node-fetch
const userID = context.params.event.author.id;
const channelID = context.params.event.channel_id;
const attachment = context.params.event.attachments[0];
if (!attachment) return lib.discord.channels['@0.3.4'].messages.create({
channel_id: channelID, content: `<@${userID}> please attach an image to your message.`
});
await lib.discord.channels.typing.create({ channel_id: channelID });
try {
const result = await fetch(attachment.url);
const buffer = await result.buffer();
const canvas = createCanvas(attachment.width, attachment.height);
const ctx = canvas.getContext('2d');
let img = await loadImage(buffer);
ctx.drawImage(img, 0, 0);
// Invert the colors of the canvas
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
data[i] = 255 - data[i]; // red
data[i + 1] = 255 - data[i + 1]; // green
data[i + 2] = 255 - data[i + 2]; // blue
}
ctx.putImageData(imageData, 0, 0);
// Send the image back to the user
const outputBuffer = canvas.toBuffer();
return lib.discord.channels['@0.3.2'].messages.create({
channel_id: channelID, content: `<@${userID}> here is your inverted image!`,
attachments: [
{
filename: `img.png`,
file: outputBuffer
}
]
});
} catch (error) {
console.error(error);
return lib.discord.channels['@0.3.4'].messages.create({
channel_id: channelID, content: `${error}`
});
}