UNPKG

detritus-client

Version:

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

109 lines (108 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Components = void 0; const baseset_1 = require("../../collections/baseset"); const constants_1 = require("../../constants"); const basestructure_1 = require("../../structures/basestructure"); const actionrow_1 = require("./actionrow"); const keysComponents = new baseset_1.BaseSet([ constants_1.DiscordKeys.COMPONENTS, constants_1.DiscordKeys.ID, constants_1.DiscordKeys.TIMEOUT, ]); /** * Utils Components Structure * @category Utils */ class Components extends basestructure_1.Structure { constructor(data = {}) { super(); this._keys = keysComponents; this.components = []; this.timeout = 10 * (60 * 1000); // 10 minutes this.merge(data); this.run = data.run || this.run; this.onError = data.onError || this.onError; this.onTimeout = data.onTimeout || this.onTimeout; } addActionRow(data = {}) { if (data instanceof actionrow_1.ComponentActionRow) { this.components.push(data); } else { this.createActionRow(data); } return this; } addButton(data = {}, inline = true) { let actionRow; if (inline) { actionRow = this.components.find((row) => row.isEmpty || !row.isFull) || this.createActionRow(); } else { actionRow = this.createActionRow(); } actionRow.addButton(data); return this; } addSelectMenu(data = {}) { const actionRow = this.createActionRow(); actionRow.addSelectMenu(data); return this; } createActionRow(data = {}) { const actionRow = new actionrow_1.ComponentActionRow(data); this.components.push(actionRow); return actionRow; } createButton(data = {}, inline = true) { let actionRow; if (inline) { actionRow = this.components.find((row) => row.isEmpty || !row.isFull) || this.createActionRow(); } else { actionRow = this.createActionRow(); } return actionRow.createButton(data); } createSelectMenu(data = {}) { const actionRow = this.createActionRow(); return actionRow.createSelectMenu(data); } mergeValue(key, value) { switch (key) { case constants_1.DiscordKeys.COMPONENTS: { this.components.length = 0; for (let raw of value) { if (raw instanceof actionrow_1.ComponentActionRow) { this.components.push(raw); } else { switch (raw.type) { case constants_1.MessageComponentTypes.ACTION_ROW: { const component = new actionrow_1.ComponentActionRow(raw); this.components.push(component); } ; break; default: { throw new Error(`Unknown component type ${raw.type}`); } ; } } } } ; return; } return super.mergeValue(key, value); } toJSON() { return this.components.map((component) => component.toJSON()); } } exports.Components = Components;