Download src/utils/canvas/help.js from vanducdev/botzalo: direct link, hf CLI and curl.
- Browser
- Download file 4.65 kB
-
https://huggingface.co/spaces/vanducdev/botzalo/resolve/main/src/utils/canvas/help.js
- Command line
-
hf download hf://spaces/vanducdev/botzalo/src/utils/canvas/help.js
-
curl -L -o help.js https://huggingface.co/spaces/vanducdev/botzalo/resolve/main/src/utils/canvas/help.js
4.65 kB
| import { createCanvas, loadImage } from "canvas"; | |
| import fs from "fs"; | |
| import path from "path"; | |
| import * as cv from "./index.js"; | |
| // Tạo Hình Lệnh !Help | |
| export async function createInstructionsImage(helpContent, isAdminBox, width = 800) { | |
| const ctxTemp = createCanvas(999, 999).getContext("2d"); | |
| const space = 36; | |
| let yTemp = 60; | |
| ctxTemp.font = "bold 28px Tahoma"; | |
| for (const key in helpContent.allMembers) { | |
| if (helpContent.allMembers.hasOwnProperty(key)) { | |
| const keyHelpContent = `${helpContent.allMembers[key].icon} ${helpContent.allMembers[key].command}`; | |
| const labelWidth = ctxTemp.measureText(keyHelpContent).width; | |
| const valueHelpContent = " -> " + helpContent.allMembers[key].description; | |
| const lineWidth = labelWidth + space + ctxTemp.measureText(valueHelpContent).width; | |
| if (lineWidth > width) { | |
| yTemp += 52; | |
| } | |
| yTemp += 52; | |
| } | |
| } | |
| yTemp += 60; // Khoảng Cách Dưới | |
| if (isAdminBox) { | |
| for (const key in helpContent.admin) { | |
| if (helpContent.admin.hasOwnProperty(key)) { | |
| const keyHelpContent = `${helpContent.admin[key].icon} ${helpContent.admin[key].command}`; | |
| const labelWidth = ctxTemp.measureText(keyHelpContent).width; | |
| const valueHelpContent = " -> " + helpContent.admin[key].description; | |
| const lineWidth = labelWidth + space + ctxTemp.measureText(valueHelpContent).width; | |
| if (lineWidth > width) { | |
| yTemp += 52; | |
| } | |
| yTemp += 52; | |
| } | |
| } | |
| yTemp += 60; // Khoảng Cách Dưới | |
| } | |
| const height = yTemp > 430 ? yTemp : 430; | |
| const canvas = createCanvas(width, height); | |
| const ctx = canvas.getContext("2d"); | |
| const backgroundGradient = ctx.createLinearGradient(0, 0, 0, height); | |
| backgroundGradient.addColorStop(0, "#0A0A0A"); // Đen đậm hơn (gần như đen thuần) | |
| backgroundGradient.addColorStop(1, "#121212"); // Đen đậm hơn nhưng có chút sắc xám | |
| ctx.fillStyle = backgroundGradient; | |
| ctx.fillRect(0, 0, width, height); | |
| let y = 60; | |
| ctx.textAlign = "left"; | |
| ctx.font = "bold 28px Tahoma"; | |
| ctx.fillStyle = cv.getRandomGradient(ctx, width); | |
| ctx.fillText(helpContent.title, space, y); | |
| y += 50; | |
| ctx.textAlign = "left"; | |
| ctx.font = "bold 28px Tahoma"; | |
| ctx.fillStyle = "#FFFFFF"; | |
| for (const key in helpContent.allMembers) { | |
| if (helpContent.allMembers.hasOwnProperty(key)) { | |
| ctx.fillStyle = cv.getRandomGradient(ctx, width); | |
| const keyHelpContent = `${helpContent.allMembers[key].icon} ${helpContent.allMembers[key].command}`; | |
| const labelWidth = ctx.measureText(keyHelpContent).width; | |
| ctx.fillText(keyHelpContent, space, y); | |
| ctx.fillStyle = "#FFFFFF"; | |
| const valueHelpContent = " -> " + helpContent.allMembers[key].description; | |
| const lineWidth = labelWidth + space + ctx.measureText(valueHelpContent).width; | |
| if (lineWidth > width) { | |
| y += 52; | |
| ctx.fillText(valueHelpContent, space + 20, y); | |
| } else { | |
| ctx.fillText(valueHelpContent, space + labelWidth, y); | |
| } | |
| y += 52; | |
| } | |
| } | |
| if (isAdminBox) { | |
| if (Object.keys(helpContent.admin).length > 0) { | |
| y += 30; | |
| ctx.textAlign = "left"; | |
| ctx.font = "bold 28px Tahoma"; | |
| ctx.fillStyle = cv.getRandomGradient(ctx, width); | |
| ctx.fillText(helpContent.titleAdmin, space, y); | |
| y += 50; | |
| for (const key in helpContent.admin) { | |
| if (helpContent.admin.hasOwnProperty(key)) { | |
| ctx.fillStyle = cv.getRandomGradient(ctx, width); | |
| const keyHelpContent = `${helpContent.admin[key].icon} ${helpContent.admin[key].command}`; | |
| const labelWidth = ctx.measureText(keyHelpContent).width; | |
| ctx.fillText(keyHelpContent, space, y); | |
| ctx.fillStyle = "#FFFFFF"; | |
| const valueHelpContent = " -> " + helpContent.admin[key].description; | |
| const lineWidth = labelWidth + space + ctx.measureText(valueHelpContent).width; | |
| if (lineWidth > width) { | |
| y += 52; | |
| ctx.fillText(valueHelpContent, space + 20, y); | |
| } else { | |
| ctx.fillText(valueHelpContent, space + labelWidth, y); | |
| } | |
| y += 52; | |
| } | |
| } | |
| } | |
| } | |
| const filePath = path.resolve(`./assets/temp/help_${Date.now()}.png`); | |
| const out = fs.createWriteStream(filePath); | |
| const stream = canvas.createPNGStream(); | |
| stream.pipe(out); | |
| return new Promise((resolve, reject) => { | |
| out.on("finish", () => resolve(filePath)); | |
| out.on("error", reject); | |
| }); | |
| } | |