@delirius/welcard
Version:
WelCard is a lightweight and futuristic welcome card library designed for WhatsApp Bots.
159 lines (158 loc) • 7.48 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WelCard = void 0;
const canvas_1 = __importDefault(require("@napi-rs/canvas"));
const colorFetch_1 = require("../functions/colorFetch");
const path_1 = __importDefault(require("path"));
canvas_1.default.GlobalFonts.registerFromPath(path_1.default.join(__dirname, "..", "..", "res", "fonts", "circularstd-black.otf"), "circular-std");
canvas_1.default.GlobalFonts.registerFromPath(path_1.default.join(__dirname, "..", "..", "res", "fonts", "notosans-jp-black.ttf"), "noto-sans-jp");
canvas_1.default.GlobalFonts.registerFromPath(path_1.default.join(__dirname, "..", "..", "res", "fonts", "notosans-black.ttf"), "noto-sans");
canvas_1.default.GlobalFonts.registerFromPath(path_1.default.join(__dirname, "..", "..", "res", "fonts", "notoemoji-bold.ttf"), "noto-emoji");
canvas_1.default.GlobalFonts.registerFromPath(path_1.default.join(__dirname, "..", "..", "res", "fonts", "notosans-kr-black.ttf"), "noto-sans-kr");
/**
* Class representing a personalized welcome card.
*/
class WelCard {
constructor() {
this.name = null;
this.author = null;
this.color = null;
this.brightness = null;
this.thumbnail = null;
this.server = null;
}
/**
* Sets the name for the welcome card.
*
* @param name - The name to display on the card.
* @returns The instance of the WelCard class for method chaining.
*/
setName(name) {
this.name = name;
return this;
}
/**
* Sets the author for the welcome card.
*
* @param author - The author to display on the card.
* @returns The instance of the WelCard class for method chaining.
*/
setAuthor(author) {
this.author = author;
return this;
}
/**
* Sets the color for the welcome card.
*
* @param color - The color to use for text.
* @returns The instance of the WelCard class for method chaining.
*/
setColor(color) {
this.color = color;
return this;
}
/**
* Sets the brightness adjustment for the welcome card.
*
* @param brightness - The brightness adjustment value.
* @returns The instance of the WelCard class for method chaining.
*/
setBrightness(brightness) {
this.brightness = brightness;
return this;
}
/**
* Sets the thumbnail image URL for the welcome card.
*
* @param thumbnail - The URL of the thumbnail image.
* @returns The instance of the WelCard class for method chaining.
*/
setThumbnail(thumbnail) {
this.thumbnail = thumbnail;
return this;
}
/**
* Sets the server information for the welcome card.
*
* @param server - The server information to display.
* @returns The instance of the WelCard class for method chaining.
*/
setServer(server) {
this.server = server;
return this;
}
/**
* Builds and generates the personalized welcome card image.
*
* @returns A promise that resolves to a PNG image buffer of the welcome card.
* @throws Error if required parameters (name or author) are missing.
*/
build() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.name) {
throw new Error("Missing name parameter");
}
if (!this.author) {
throw new Error("Missing author parameter");
}
let thumbnailURL = this.thumbnail;
const validatedColor = yield (0, colorFetch_1.colorFetch)(this.color || "ff0000", this.brightness || 0, thumbnailURL);
const img = yield canvas_1.default.loadImage("https://i.postimg.cc/XYLb4bsB/background.png");
const thumbnailCanvas = canvas_1.default.createCanvas(564, 564);
const thumbnailCtx = thumbnailCanvas.getContext("2d");
const thumbnailImage = yield canvas_1.default.loadImage(thumbnailURL, {
requestOptions: {
headers: {
"User-Agent": "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)",
},
},
});
const thumbnailSize = Math.min(thumbnailImage.width, thumbnailImage.height);
const thumbnailX = (thumbnailImage.width - thumbnailSize) / 2;
const thumbnailY = (thumbnailImage.height - thumbnailSize) / 2;
thumbnailCtx.beginPath();
const cornerRadius2 = 45;
thumbnailCtx.moveTo(0 + cornerRadius2, 0);
thumbnailCtx.arcTo(thumbnailCanvas.width, 0, thumbnailCanvas.width, thumbnailCanvas.height, cornerRadius2);
thumbnailCtx.arcTo(thumbnailCanvas.width, thumbnailCanvas.height, 0, thumbnailCanvas.height, cornerRadius2);
thumbnailCtx.arcTo(0, thumbnailCanvas.height, 0, 0, cornerRadius2);
thumbnailCtx.arcTo(0, 0, thumbnailCanvas.width, 0, cornerRadius2);
thumbnailCtx.closePath();
thumbnailCtx.clip();
thumbnailCtx.drawImage(thumbnailImage, thumbnailX, thumbnailY, thumbnailSize, thumbnailSize, 0, 0, thumbnailCanvas.width, thumbnailCanvas.height);
if (this.name && this.name.length > 15)
this.name = `${this.name.slice(0, 15)}...`;
if (this.author && this.author.length > 15)
this.author = `${this.author.slice(0, 15)}...`;
if (this.server && this.server.length > 15)
this.server = `${this.server.slice(0, 15)}...`;
const image = canvas_1.default.createCanvas(1280, 450);
const ctx = image.getContext("2d");
ctx.drawImage(img, 0, 0, 1280, 450);
ctx.fillStyle = `#${validatedColor}`;
ctx.font = `75px circular-std, noto-emoji, noto-sans-jp, noto-sans, noto-sans-kr`;
ctx.fillText(this.name || "", 70, 120);
ctx.fillStyle = "#b8b8b8";
ctx.font = `50px circular-std, noto-emoji, noto-sans-jp, noto-sans, noto-sans-kr`;
ctx.fillText(this.author || "", 75, 190);
ctx.fillStyle = "#b8b8b8";
ctx.font = `30px circular-std, noto-emoji, noto-sans-jp, noto-sans, noto-sans-kr`;
ctx.fillText(this.server || "", 75, 380);
ctx.drawImage(thumbnailCanvas, 837, 8, 435, 435);
return image.toBuffer("image/png");
});
}
}
exports.WelCard = WelCard;