File size: 5,718 Bytes
23bbc26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const moment = require("moment-timezone");
const fs = require("fs-extra");
const axios = require("axios");
const cheerio = require("cheerio");
const Canvas = require("canvas");
const https = require("https");
const agent = new https.Agent({
	rejectUnauthorized: false
});
const { getStreamFromURL } = global.utils;

module.exports = {
	config: {
		name: "moon",
		version: "1.3",
		author: "NTKhang",
		countDown: 5,
		role: 0,
		shortDescription: {
			vi: "xem ảnh mặt trăng",
			en: "view moon image"
		},
		longDescription: {
			vi: "xem ảnh mặt trăng vào đêm bạn chọn (dd/mm/yyyy)",
			en: "view moon image on the night you choose (dd/mm/yyyy)"
		},
		category: "image",
		guide: {
			vi: "  {pn} <ngày/tháng/năm>"
				+ "\n   {pn} <ngày/tháng/năm> <caption>",
			en: "  {pn} <day/month/year>"
				+ "\n   {pn} <day/month/year> <caption>"
		}
	},

	langs: {
		vi: {
			invalidDateFormat: "Vui lòng nhập ngày/tháng/năm hợp lệ theo định dạng DD/MM/YYYY",
			error: "Đã xảy ra lỗi không thể lấy ảnh mặt trăng của ngày %1",
			invalidDate: "Ngày %1 không hợp lệ",
			caption: "- Ảnh mặt trăng vào đêm %1"
		},
		en: {
			invalidDateFormat: "Please enter a valid date in DD/MM/YYYY format",
			error: "An error occurred while getting the moon image of %1",
			invalidDate: "%1 is not a valid date",
			caption: "- Moon image on %1"
		}
	},

	onStart: async function ({ args, message, getLang }) {
		const date = checkDate(args[0]);
		if (!date)
			return message.reply(getLang("invalidDateFormat"));
		const linkCrawl = `https://lunaf.com/lunar-calendar/${date}`;

		let html;
		try {
			html = await axios.get(linkCrawl, { httpsAgent: agent });
		}
		catch (err) {
			return message.reply(getLang("error", args[0]));
		}

		const $ = cheerio.load(html.data);
		const href = $("figure img").attr("data-ezsrcset");
		const number = href.match(/phase-(\d+)\.png/)[1];
		const imgSrc = moonImages[Number(number)];
		const { data: imgSrcBuffer } = await axios.get(imgSrc, {
			responseType: "arraybuffer"
		});

		const msg = getLang("caption", args[0])
			+ `\n- ${$($('h3').get()[0]).text()}`
			+ `\n- ${$("#phimg > small").text()}`
			+ `\n- ${linkCrawl}`
			+ `\n- https://lunaf.com/img/moon/h-phase-${number}.png`;

		if (args[1]) {
			const canvas = Canvas.createCanvas(1080, 2400);
			const ctx = canvas.getContext("2d");
			ctx.fillStyle = "black";
			ctx.fillRect(0, 0, 1080, 2400);

			const moon = await Canvas.loadImage(imgSrcBuffer);
			centerImage(ctx, moon, 1080 / 2, 2400 / 2, 970, 970);

			ctx.font = "60px \"Kanit SemiBold\"";
			const wrapText = getLines(ctx, args.slice(1).join(" "), 594);
			ctx.textAlign = "center";
			ctx.fillStyle = "white";

			const yStartText = 2095;//2042;
			//ctx.fillRect(0, 2042, 1080, 5);
			let heightText = yStartText - wrapText.length / 2 * 75;
			for (const text of wrapText) {
				ctx.fillText(text, 750, heightText);
				heightText += 75;
			}

			const pathSave = __dirname + "/tmp/wallMoon.png";
			fs.writeFileSync(pathSave, canvas.toBuffer());
			message.reply({
				body: msg,
				attachment: fs.createReadStream(pathSave)
			}, () => fs.unlinkSync(pathSave));
		}
		else {
			const streamImg = await getStreamFromURL(imgSrc);
			message.reply({
				body: msg,
				attachment: streamImg
			});
		}
	}
};


const pathFont = __dirname + "/assets/font/Kanit-SemiBoldItalic.ttf";
Canvas.registerFont(pathFont, {
	family: "Kanit SemiBold"
});

function getLines(ctx, text, maxWidth) {
	const words = text.split(" ");
	const lines = [];
	let currentLine = words[0];
	for (let i = 1; i < words.length; i++) {
		const word = words[i];
		const width = ctx.measureText(`${currentLine} ${word}`).width;
		if (width < maxWidth) {
			currentLine += " " + word;
		}
		else {
			lines.push(currentLine);
			currentLine = word;
		}
	}
	lines.push(currentLine);
	return lines;
}

function centerImage(ctx, img, x, y, sizeX, sizeY) {
	ctx.drawImage(img, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
}

function checkDate(date) {
	const [day0, month0, year0] = (date || "").split('/');
	const day = (day0 || "").length == 1 ? "0" + day0 : day0;
	const month = (month0 || "").length == 1 ? "0" + month0 : month0;
	const year = year0 || "";
	const newDateFormat = year + "/" + month + "/" + day;
	return moment(newDateFormat, 'YYYY/MM/DD', true).isValid() ? newDateFormat : false;
}

const moonImages = [
	'https://i.ibb.co/9shyYH1/moon-0.png',
	'https://i.ibb.co/vBXLL37/moon-1.png',
	'https://i.ibb.co/0QCKK9D/moon-2.png',
	'https://i.ibb.co/Dp62X2j/moon-3.png',
	'https://i.ibb.co/xFKCtfd/moon-4.png',
	'https://i.ibb.co/m4L533L/moon-5.png',
	'https://i.ibb.co/VmshdMN/moon-6.png',
	'https://i.ibb.co/4N7R2B2/moon-7.png',
	'https://i.ibb.co/C2k4YB8/moon-8.png',
	'https://i.ibb.co/F62wHxP/moon-9.png',
	'https://i.ibb.co/Gv6R1mk/moon-10.png',
	'https://i.ibb.co/0ZYY7Kk/moon-11.png',
	'https://i.ibb.co/KqXC5F5/moon-12.png',
	'https://i.ibb.co/BGtLpRJ/moon-13.png',
	'https://i.ibb.co/jDn7pPx/moon-14.png',
	'https://i.ibb.co/kykn60t/moon-15.png',
	'https://i.ibb.co/qD4LFLs/moon-16.png',
	'https://i.ibb.co/qJm9gcQ/moon-17.png',
	'https://i.ibb.co/yYFYZx9/moon-18.png',
	'https://i.ibb.co/8bc7vpZ/moon-19.png',
	'https://i.ibb.co/jHG7DKs/moon-20.png',
	'https://i.ibb.co/5WD18Rn/moon-21.png',
	'https://i.ibb.co/3Y06yHM/moon-22.png',
	'https://i.ibb.co/4T8Zdfy/moon-23.png',
	'https://i.ibb.co/n1CJyP4/moon-24.png',
	'https://i.ibb.co/zFwJRqz/moon-25.png',
	'https://i.ibb.co/gVBmMCW/moon-26.png',
	'https://i.ibb.co/hRY89Hn/moon-27.png',
	'https://i.ibb.co/7C13s7Z/moon-28.png',
	'https://i.ibb.co/2hDTwB4/moon-29.png',
	'https://i.ibb.co/Rgj9vpj/moon-30.png',
	'https://i.ibb.co/s5z0w9R/moon-31.png'
];