← back to browse
goat command

love

No description

0
views
0
likes
0
installs
raw source
const { createCanvas, loadImage } = require("canvas");
const axios = require("axios");
const fs = require("fs");

const FRAME_URL = "https://i.ibb.co.com/hRC2HcRx/297-2972575-i-love-you-valentine-s-day-frame-love.jpg";
const FRAME_W = 268;
const FRAME_H = 280;
const SCALE = 4;

const LEFT_SLOT = { x: 108 * SCALE, y: 78 * SCALE, w: 44 * SCALE, h: 118 * SCALE };
const RIGHT_SLOT = { x: 172 * SCALE, y: 76 * SCALE, w: 52 * SCALE, h: 126 * SCALE };

module.exports = {
	config: {
		name: "love",
		version: "3.0.0",
		author: "EryXenX",
		countDown: 5,
		role: 0,
		description: {
			en: "Create a love album with a mentioned or replied user",
			vi: "Create a love album with a mentioned or replied user"
		},
		category: "fun",
		guide: {
			en: "{pn}: reply or mention someone to create a love album"
		}
	},

	onStart: async function ({ api, event, usersData }) {
		const { threadID, messageID, senderID, mentions, messageReply } = event;

		let targetID;
		if (messageReply) {
			targetID = messageReply.senderID;
		} else if (mentions && Object.keys(mentions).length > 0) {
			targetID = Object.keys(mentions)[0];
		}

		if (!targetID) {
			return api.sendMessage("Reply to someone's message or mention them to create a love album.", threadID, messageID);
		}

		if (targetID === senderID) {
			return api.sendMessage("You can't create a love album with yourself.", threadID, messageID);
		}

		const senderAvatarUrl = `https://graph.facebook.com/${senderID}/picture?width=720&height=720&access_token=6628568379%7Cc1e620fa708a1d5696fb991c1bde5662`;
		const targetAvatarUrl = `https://graph.facebook.com/${targetID}/picture?width=720&height=720&access_token=6628568379%7Cc1e620fa708a1d5696fb991c1bde5662`;

		const [senderName, targetName] = await Promise.all([
			usersData.getName(senderID),
			usersData.getName(targetID)
		]);

		const [senderRes, targetRes, frameRes] = await Promise.all([
			axios.get(senderAvatarUrl, { responseType: "arraybuffer" }),
			axios.get(targetAvatarUrl, { responseType: "arraybuffer" }),
			axios.get(FRAME_URL, { responseType: "arraybuffer" })
		]);

		const senderAvatar = await loadImage(Buffer.from(senderRes.data));
		const targetAvatar = await loadImage(Buffer.from(targetRes.data));
		const rawFrame = await loadImage(Buffer.from(frameRes.data));

		const frameCanvas = createCanvas(FRAME_W, FRAME_H);
		const fctx = frameCanvas.getContext("2d");
		fctx.drawImage(rawFrame, 0, 0, FRAME_W, FRAME_H);
		const imgData = fctx.getImageData(0, 0, FRAME_W, FRAME_H);
		const data = imgData.data;
		for (let i = 0; i < data.length; i += 4) {
			const r = data[i], g = data[i + 1], b = data[i + 2];
			const dist = Math.abs(r - 246) + Math.abs(g - 246) + Math.abs(b - 246);
			if (dist < 12) data[i + 3] = 0;
		}
		fctx.putImageData(imgData, 0, 0);

		const outW = FRAME_W * SCALE;
		const outH = FRAME_H * SCALE;
		const canvas = createCanvas(outW, outH);
		const ctx = canvas.getContext("2d");

		function drawCover(img, box) {
			const ratio = Math.max(box.w / img.width, box.h / img.height);
			const dw = img.width * ratio;
			const dh = img.height * ratio;
			const dx = box.x + (box.w - dw) / 2;
			const dy = box.y + (box.h - dh) / 2;
			ctx.drawImage(img, dx, dy, dw, dh);
		}

		ctx.save();
		ctx.beginPath();
		ctx.rect(LEFT_SLOT.x, LEFT_SLOT.y, LEFT_SLOT.w, LEFT_SLOT.h);
		ctx.clip();
		drawCover(senderAvatar, LEFT_SLOT);
		ctx.restore();

		ctx.save();
		ctx.beginPath();
		ctx.rect(RIGHT_SLOT.x, RIGHT_SLOT.y, RIGHT_SLOT.w, RIGHT_SLOT.h);
		ctx.clip();
		drawCover(targetAvatar, RIGHT_SLOT);
		ctx.restore();

		ctx.drawImage(frameCanvas, 0, 0, outW, outH);

		ctx.textAlign = "center";
		ctx.font = `bold ${18 * SCALE}px sans-serif`;
		ctx.fillStyle = "#ffffff";
		ctx.strokeStyle = "#c92a6d";
		ctx.lineWidth = 4;
		const captionY = outH - 10;
		ctx.strokeText(`${senderName} ❤ ${targetName}`, outW / 2, captionY);
		ctx.fillText(`${senderName} ❤ ${targetName}`, outW / 2, captionY);

		const buffer = canvas.toBuffer("image/png");
		const path = __dirname + `/cache/love_${senderID}_${targetID}.png`;
		fs.writeFileSync(path, buffer);

		return api.sendMessage(
			{
				body: `${senderName} ❤ ${targetName}`,
				attachment: fs.createReadStream(path)
			},
			threadID,
			() => fs.unlinkSync(path),
			messageID
		);
	}
};

View raw file