UNPKG

detritus-client

Version:

A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.

127 lines (126 loc) 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentActionRow = void 0; const baseset_1 = require("../../collections/baseset"); const constants_1 = require("../../constants"); const basestructure_1 = require("../../structures/basestructure"); const button_1 = require("./button"); const selectmenu_1 = require("./selectmenu"); const keysComponentActionRow = new baseset_1.BaseSet([ constants_1.DiscordKeys.COMPONENTS, constants_1.DiscordKeys.TYPE, ]); /** * Utils Component Action Row Structure * @category Utils */ class ComponentActionRow extends basestructure_1.Structure { constructor(data = {}) { super(); this._keys = keysComponentActionRow; this.components = []; this.type = constants_1.MessageComponentTypes.ACTION_ROW; this.merge(data); this.type = constants_1.MessageComponentTypes.ACTION_ROW; } get hasButton() { for (let component of this.components) { if (component.type === constants_1.MessageComponentTypes.BUTTON) { return true; } } return false; } get hasRun() { return this.components.some((component) => typeof (component.run) === 'function'); } get hasSelectMenu() { for (let component of this.components) { if (component.type === constants_1.MessageComponentTypes.SELECT_MENU) { return true; } } return false; } get isEmpty() { return !this.components.length; } get isFull() { if (this.hasSelectMenu) { return constants_1.MAX_ACTION_ROW_SELECT_MENUS <= this.components.length; } else if (this.hasButton) { return constants_1.MAX_ACTION_ROW_BUTTONS <= this.components.length; } return false; } addButton(data = {}) { if (data instanceof button_1.ComponentButton) { return this.addComponent(data); } return this.addComponent(new button_1.ComponentButton(data)); } addComponent(component) { this.components.push(component); return this; } addSelectMenu(data = {}) { if (data instanceof selectmenu_1.ComponentSelectMenu) { return this.addComponent(data); } return this.addComponent(new selectmenu_1.ComponentSelectMenu(data)); } createButton(data = {}) { const component = new button_1.ComponentButton(data); this.addComponent(component); return component; } createSelectMenu(data = {}) { const component = new selectmenu_1.ComponentSelectMenu(data); this.addComponent(component); return component; } mergeValue(key, value) { switch (key) { case constants_1.DiscordKeys.COMPONENTS: { this.components.length = 0; for (let raw of value) { if (raw instanceof button_1.ComponentButton || raw instanceof selectmenu_1.ComponentSelectMenu) { this.components.push(raw); } else { switch (raw.type) { case constants_1.MessageComponentTypes.BUTTON: { const component = new button_1.ComponentButton(raw); this.components.push(component); } ; break; case constants_1.MessageComponentTypes.SELECT_MENU: { const component = new selectmenu_1.ComponentSelectMenu(raw); this.components.push(component); } ; break; default: { throw new Error(`Unknown component type ${raw.type}`); } ; } } } } ; return; } return super.mergeValue(key, value); } toJSON() { return super.toJSON(); } } exports.ComponentActionRow = ComponentActionRow;