UNPKG

@jadl/cmd

Version:
81 lines (80 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Section = exports.ButtonMenu = void 0; const builders_1 = require("@jadl/builders"); const v9_1 = require("discord-api-types/v9"); const WorkerInject_1 = require("../structures/WorkerInject"); const sectionsSymbol = Symbol.for('button menu sections'); class ButtonMenu extends WorkerInject_1.WorkerInject { async _onInteraction(int, worker) { if (int.type !== v9_1.InteractionType.MessageComponent) return; if (int.data.component_type !== v9_1.ComponentType.Button) return; let [meta, ...splitData] = int.data.custom_id.split('#'); const data = splitData.join('#'); const [bm, menu, method] = meta.split('_'); if (bm !== 'BM') return; if (menu !== this.constructor.name) return; const section = this[sectionsSymbol].find((x) => x.method === method); if (!section) return; await worker.api .post(v9_1.Routes.interactionCallback(int.id, int.token), { body: { type: v9_1.InteractionResponseType.DeferredMessageUpdate } }) .catch(() => { }); const dataObject = Object.fromEntries(new URLSearchParams(data)); const res = await this[section.method](dataObject, int); const messageBuilder = (0, builders_1.parseToMessageBuilder)(res); messageBuilder.message.components = []; this.createButtons(section.method, dataObject, messageBuilder); await worker.api .patch(v9_1.Routes.webhookMessage(int.application_id, int.token, int.message.id), (0, builders_1.parse)(messageBuilder)) .catch(() => { }); } createButtons(selectedMethod, data, builder) { const rows = this[sectionsSymbol].reduce((buttons, section) => { if (!buttons[section.options.row]) buttons[section.options.row] = []; buttons[section.options.row].push({ ...section.button, custom_id: section.button.custom_id + '#' + new URLSearchParams(data).toString(), disabled: section.button.custom_id.split('_')[2] === selectedMethod || !!section.options.disableIf?.some((x) => data[x]) }); return buttons; }, []); rows.forEach((x) => builder.addComponentRow(x)); } async start(section, data, interaction) { const res = await this[section](data, interaction); const messageBuilder = (0, builders_1.parseToMessageBuilder)(res); messageBuilder.message.components = []; this.createButtons(section, data, messageBuilder); return messageBuilder; } } exports.ButtonMenu = ButtonMenu; const Section = (button, options = { row: 0 }) => { return (target, prop) => { if (!target[sectionsSymbol]) target[sectionsSymbol] = []; target[sectionsSymbol].push({ method: prop, button: { ...button, type: v9_1.ComponentType.Button, custom_id: `BM_${target.constructor.name}_${prop}` }, options }); }; }; exports.Section = Section;