UNPKG

@jadl/builders

Version:
187 lines (186 loc) 5.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Embed = void 0; const colors_1 = __importDefault(require("../utils/colors")); /** * Discord Embed */ class Embed { constructor(obj = Embed.default.clone().render()) { this.obj = obj; } /** * Sets the color * @param color Color hex code */ color(color) { var _a; this.obj.color = (_a = colors_1.default[color]) !== null && _a !== void 0 ? _a : color; return this; } /** * Sets author * @param name Name of author * @param icon Author avatar icon * @param url URL anchored to the author name */ author(name, icon, url) { this.obj.author = { name, icon_url: icon, url }; return this; } /** * Sets the title * @param title Title name * @param url URL anchored to title name */ title(title, url) { if (title) this.obj.title = title; if (url) this.obj.url = url; return this; } /** * Sets description * @param desc Description */ description(desc) { this.obj.description = desc; return this; } /** * Adds a field * @param name Fields title * @param value Fields value * @param inline Whether the field is inline */ field(name, value, inline) { if (!this.obj.fields) this.obj.fields = []; this.obj.fields.push({ name, value, inline }); return this; } /** * Sets the thumbnail * @param url URL of thumbnail * @param width Optional fixed width * @param height Optional fixed height */ thumbnail(url, width, height) { this.obj.thumbnail = { url, width, height }; return this; } /** * Sets the image * @param url URL of image * @param width Optional fixed width * @param height Optional fixed height */ image(url, width, height) { this.obj.image = { url, width, height }; return this; } /** * Sets the video * @param url URL of video * @param width Optional fixed width * @param height Optional fixed height */ video(url, width, height) { this.obj.video = { url, width, height }; return this; } /** * Sets the footer * @param text Text for footer * @param icon Small icon on the bottom left */ footer(text, icon) { if (!this.obj.footer) this.obj.footer = { text: '' }; if (text) this.obj.footer.text = text; if (icon) this.obj.footer.icon_url = icon; return this; } /** * Sets the timestamp * @param date Date to set, leave blank for current time */ timestamp(date = new Date()) { this.obj.timestamp = date.toISOString(); return this; } clone() { return new Embed(JSON.parse(JSON.stringify(this.obj))); } /** * Total length of the embed of combined character elements (should not exceed 6000) */ get length() { var _a, _b, _c, _d, _e, _f, _g; return ((((_a = this.obj.title) === null || _a === void 0 ? void 0 : _a.length) || 0) + (((_b = this.obj.description) === null || _b === void 0 ? void 0 : _b.length) || 0) + (((_c = this.obj.fields) === null || _c === void 0 ? void 0 : _c.reduce((a, b) => a + b.name.length + b.value.length, 0)) || 0) + (((_e = (_d = this.obj.footer) === null || _d === void 0 ? void 0 : _d.text) === null || _e === void 0 ? void 0 : _e.length) || 0) + (((_g = (_f = this.obj.author) === null || _f === void 0 ? void 0 : _f.name) === null || _g === void 0 ? void 0 : _g.length) || 0)); } /** * Tests the embeds values for exceeded limits * @returns Whether or not a part of the embed exceeds it's limits */ exceedsLimits() { var _a, _b, _c, _d, _e, _f, _g, _h; return (this.length > Embed.LIMITS.TOTAL_LENGTH || (((_a = this.obj.title) === null || _a === void 0 ? void 0 : _a.length) || 0) > Embed.LIMITS.TITLE_LENGTH || (((_b = this.obj.description) === null || _b === void 0 ? void 0 : _b.length) || 0) > Embed.LIMITS.DESCRIPTION_LENGTH || (((_c = this.obj.fields) === null || _c === void 0 ? void 0 : _c.length) || 0) > Embed.LIMITS.FIELDS || ((_d = this.obj.fields) === null || _d === void 0 ? void 0 : _d.some((field) => field.name.length > Embed.LIMITS.FIELD_NAME_LENGTH || field.value.length > Embed.LIMITS.FIELD_VALUE_LENGTH)) || (((_f = (_e = this.obj.footer) === null || _e === void 0 ? void 0 : _e.text) === null || _f === void 0 ? void 0 : _f.length) || 0) > Embed.LIMITS.FOOTER_TEXT_LENGTH || (((_h = (_g = this.obj.author) === null || _g === void 0 ? void 0 : _g.name) === null || _h === void 0 ? void 0 : _h.length) || 0) > Embed.LIMITS.AUTHOR_NAME_LENGTH); } /** * Renders the embed * @returns */ render() { return this.obj; } } exports.Embed = Embed; Embed.default = new Embed({}); Embed.LIMITS = { TITLE_LENGTH: 256, DESCRIPTION_LENGTH: 4096, FIELD_NAME_LENGTH: 256, FIELD_VALUE_LENGTH: 1024, FOOTER_TEXT_LENGTH: 2048, AUTHOR_NAME_LENGTH: 256, TOTAL_LENGTH: 6000, FIELDS: 25 };