UNPKG

cordbuilder

Version:

Simple dsl to create discord builders simple.

133 lines (132 loc) 5.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const discord_js_1 = require("discord.js"); const embed_1 = require("../constants/embed"); const Parser_1 = __importDefault(require("../frontend/Parser")); const Builder_1 = __importDefault(require("./Builder")); class CordEmbed extends Builder_1.default { static from(code) { const parser = new Parser_1.default(code); const program = parser.parse(); const builder = new CordEmbed(code, program); const embed = builder.build(program); return embed; } constructor(code, program) { super(embed_1.EMBED_PROPERTIES, program, "embed", embed_1.EMBED_SUB_PROPERTIES); this.code = code; super.validateProperties(); super.validateSubProperties(); this.build(program); } build(program) { const embed = new discord_js_1.EmbedBuilder(); const properties = program.statements; // With sub-properties const author = properties.find((p) => p.identifier.name == "author"); const footer = properties.find((p) => p.identifier.name == "footer"); const fields = properties.filter((p) => p.identifier.name == "field"); if (author) this.buildAuthor(embed, author); if (footer) this.buildFooter(embed, footer); if (fields.length > 0) this.buildFields(embed, fields); // Without sub-properties const title = properties.find((p) => p.identifier.name == "title"); const description = properties.find((p) => p.identifier.name == "description"); const image = properties.find((p) => p.identifier.name == "image"); const timestamp = properties.find((p) => p.identifier.name == "timestamp"); const thumbnail = properties.find((p) => p.identifier.name == "thumbnail"); const url = properties.find((p) => p.identifier.name == "url"); const color = properties.find((p) => p.identifier.name == "color"); if (title) this.buildTitle(embed, title); if (description) this.buildDescription(embed, description); if (image) this.buildImage(embed, image); if (timestamp) this.buildTimestamp(embed, timestamp); if (thumbnail) this.buildThumb(embed, thumbnail); if (url) this.buildUrl(embed, url); if (color) this.buildColor(embed, color); return embed; } buildTitle(embed, title) { const { value } = title.expression; embed.setTitle(value.slice(1, -1)); } buildDescription(embed, description) { const { value } = description.expression; embed.setDescription(value.slice(1, -1)); } buildImage(embed, image) { const { value } = image.expression; embed.setImage(value.slice(1, -1)); } buildTimestamp(embed, timestamp) { const { value } = timestamp.expression; if (value) embed.setTimestamp(); } buildThumb(embed, thumb) { const { value } = thumb.expression; embed.setThumbnail(value.slice(1, -1)); } buildUrl(embed, url) { const { value } = url.expression; embed.setURL(value.slice(1, -1)); } buildColor(embed, color) { const { value } = color.expression; embed.setColor(value.slice(1, -1)); } buildAuthor(embed, author) { const { args } = author.expression; const nameProp = args.find((a) => a.identifier.name == "name") ?.expression; const iconProp = args.find((a) => a.identifier.name == "icon") ?.expression; const urlProp = args.find((a) => a.identifier.name == "url") ?.expression; const name = nameProp.value.slice(1, -1); const iconURL = iconProp?.value.slice(1, -1); const url = urlProp?.value.slice(1, -1); embed.setAuthor({ name, iconURL, url }); } buildFields(embed, fields) { const expressions = fields.map((field) => field.expression); const buildedFields = Array.from({ length: expressions.length }, (_, i) => { const { args } = expressions[i]; const nameProp = args.find((a) => a.identifier.name == "name") ?.expression; const valueProp = args.find((a) => a.identifier.name == "value") ?.expression; const inlineProp = args.find((a) => a.identifier.name == "inline") ?.expression; const name = nameProp.value.slice(1, -1); const value = valueProp.value.slice(1, -1); const inline = inlineProp?.value ?? false; return { name, value, inline }; }); embed.addFields(buildedFields); } buildFooter(embed, author) { const { args } = author.expression; const textProp = args.find((a) => a.identifier.name == "text") ?.expression; const iconProp = args.find((a) => a.identifier.name == "icon") ?.expression; const text = textProp.value.slice(1, -1); const iconURL = iconProp?.value.slice(1, -1); embed.setFooter({ text, iconURL }); } } exports.default = CordEmbed;