Deepfries a image sent with the 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({ channelID });
try{
const response = await fetch(attachment.url);
const buffer = await response.buffer();
const canvas = createCanvas(attachment.width, attachment.height);
const ctx = canvas.getContext('2d');
let img = await loadImage(buffer);
ctx.drawImage(img, 0, 0);
// Apply the deep frying effect to the canvas
const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixels = data.data;
for (let i = 0; i < pixels.length; i += 4) {
const red = pixels[i];
const green = pixels[i + 1];
const blue = pixels[i + 2];
const alpha = pixels[i + 3];
// Increase the red and yellow channels to give the image a "deep fried" look
pixels[i] = Math.min(255, red * 1.5);
pixels[i + 1] = Math.min(255, green * 0.8);
pixels[i + 2] = Math.min(255, blue * 0.2);
}
// Increase saturation
const saturation = 2;
const adjustment = (1 - saturation);
for (let i = 0; i < pixels.length; i += 4) {
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
const gray = 0.2989 * r + 0.5870 * g + 0.1140 * b;
pixels[i] = (r * saturation + gray * adjustment);
pixels[i + 1] = (g * saturation + gray * adjustment);
pixels[i + 2] = (b * saturation + gray * adjustment);
}
// Increase contrast
const contrast = 1.5;
const intercept = 128 * (1 - contrast);
for (let i = 0; i < pixels.length; i += 4) {
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
pixels[i] = Math.max(0, Math.min(255, contrast * r + intercept));
pixels[i + 1] = Math.max(0, Math.min(255, contrast * g + intercept));
pixels[i + 2] = Math.max(0, Math.min(255, contrast * b + intercept));
}
ctx.putImageData(data, 0, 0);
// Convert the canvas to a buffer and send it as an attachment to the Discord channel
const outputBuffer = canvas.toBuffer();
return lib.discord.channels['@0.3.2'].messages.create({
channel_id: channelID, content: `<@${userID}> here is your deepfried 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}`
});
}