cordbuilder
Version:
Simple dsl to create discord builders simple.
86 lines (85 loc) • 3.35 kB
JavaScript
;
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 button_1 = require("../constants/button");
const Parser_1 = __importDefault(require("../frontend/Parser"));
const Builder_1 = __importDefault(require("./Builder"));
class CordButton extends Builder_1.default {
static from(code) {
const parser = new Parser_1.default(code);
const program = parser.parse();
const builder = new CordButton(code, program);
const embed = builder.build(program);
return embed;
}
constructor(code, program) {
super(button_1.BUTTON_PROPERTIES, program, 'button');
this.code = code;
super.validateProperties();
}
build(program) {
const button = new discord_js_1.ButtonBuilder();
const properties = program.statements;
// Without sub-properties
const id = properties.find((p) => p.identifier.name == "id");
const label = properties.find((p) => p.identifier.name == "label");
const emoji = properties.find((p) => p.identifier.name == "emoji");
const style = properties.find((p) => p.identifier.name == "style");
const url = properties.find((p) => p.identifier.name == "url");
const disabled = properties.find((p) => p.identifier.name == "disabled");
if (id)
this.buildId(button, id);
if (label)
this.buildLabel(button, label);
if (emoji)
this.buildEmoji(button, emoji);
if (style)
this.buildStyle(button, style);
if (url)
this.buildUrl(button, url);
if (disabled)
this.buildDisabled(button, disabled);
if (!id && !url)
throw new Error('button: Buttons must have a id or an url');
if (!emoji && !label)
throw new Error('button: Buttons must have a label and/or an emoji');
if (id && url)
throw new Error('button: Buttons can\'t have a id and an url');
return button;
}
buildId(button, id) {
const { value } = id.expression;
button.setCustomId(value.slice(1, -1));
}
buildLabel(button, label) {
const { value } = label.expression;
button.setLabel(value.slice(1, -1));
}
buildEmoji(button, emoji) {
const { value } = emoji.expression;
button.setEmoji(value.slice(1, -1));
}
buildStyle(button, style) {
const { name } = style.expression;
const findStyle = Object.entries(discord_js_1.ButtonStyle)
.find(([n, s]) => n == name);
const styles = Object.keys(discord_js_1.ButtonStyle)
.filter(s => s.length > 1)
.join(' | ');
if (!findStyle?.[0])
throw new Error(`button: Invalid button (style) has provided: ${name}, expects -> (${styles})`);
button.setStyle(findStyle[1]);
}
buildUrl(button, url) {
const { value } = url.expression;
button.setURL(value.slice(1, -1));
}
buildDisabled(button, disabled) {
const { value } = disabled.expression;
button.setDisabled(value);
}
}
exports.default = CordButton;