From 9aa6c2189a24316bc4ad47b120b61efed43999be Mon Sep 17 00:00:00 2001 From: ION606 Date: Tue, 25 Apr 2023 20:33:28 -0400 Subject: [PATCH] finished Modals --- structures/interactions/Modal.js | 49 ++++++++++++++++---- structures/interactions/createInteraction.js | 4 +- tests/interactionTests.js | 17 +++++-- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/structures/interactions/Modal.js b/structures/interactions/Modal.js index 6e56637..26a6d7d 100644 --- a/structures/interactions/Modal.js +++ b/structures/interactions/Modal.js @@ -1,3 +1,4 @@ +import { MessageActionRow } from "../messages/MessageActionRow.js"; import { Interaction } from "./interaction.js"; /** @enum {number} */ @@ -41,6 +42,8 @@ export class ModalComponent { } constructor(obj) { + if (obj == undefined) return; + for (const k in this) { if (obj[k] != undefined) { this[k] = obj[k]; @@ -57,34 +60,62 @@ export class Modal extends Interaction { /** @type {String} */ custom_id; - /** @type {ModalComponent[]} */ + /** @type {Map} */ components; /** - * @param {ModalComponent} c + * @param {ModalComponent} c + * @returns {Boolean} true is added false otherwise */ addComponent(c) { - this.components.push(c); + if (!c || this.components.has(c.custom_id)) return true; + this.components.set(c.custom_id, c); + } + + /** + * @description returns the Modal's components as a map of + * + * `custom_id ==> input` + * @returns {[{value: String, custom_id: String}]} + */ + getComponents() { + const m = new Map(); + for (const k of this.components) { + m.set(k[0], k[1]); + } + return m; + } + + /** + * @description returns the component with the custom id specified + * @param {String} cid + * @returns {String} + */ + getComponent(cid) { + return this.components.get(cid).value; } toObj() { const obj = {title: this.title, custom_id: this.custom_id, components: []}; - for (const comp of this.components) { - obj.components.push(comp.toObj()); + for (const key in this.components) { + const comp = this.components.get(key); + const a = new MessageActionRow(); + a.addComponent(comp); + obj.components.push(a.toObj()); } return obj; } constructor(intRaw, client) { super(intRaw, client); - this.components = []; + this.components = new Map(); if (!intRaw) return; - // [ { value: 'nnnnnnnnnnnnn', type: 4, custom_id: 'nonononononono' } ] for (const opt of intRaw.data.components) { - //These are nested - // this.components.push(new ModalComponent()); + const compRaw = opt.components[0]; + const comp = new ModalComponent(compRaw); + this.components.set(comp.custom_id, comp.value); } } } \ No newline at end of file diff --git a/structures/interactions/createInteraction.js b/structures/interactions/createInteraction.js index 587e59b..021db07 100644 --- a/structures/interactions/createInteraction.js +++ b/structures/interactions/createInteraction.js @@ -50,8 +50,8 @@ export function createInteraction(intRaw, client) { return createSelectMenu(intRaw, client); case interactionTypes.ModalSubmit: - return console.log("MODALS NOT FULLY IMPLEMENTED!"); - // return new Modal(intRaw, client); + // return console.log("MODALS NOT FULLY IMPLEMENTED!"); + return new Modal(intRaw, client); case interactionTypes.Ping: console.log("pong"); diff --git a/tests/interactionTests.js b/tests/interactionTests.js index 2d1654f..5219a9f 100644 --- a/tests/interactionTests.js +++ b/tests/interactionTests.js @@ -14,12 +14,17 @@ export default async (interaction) => { c.custom_id = 'nonononononono'; c.label = "hi"; c.style = 1; + + const c2 = new ModalComponent(); + c2.custom_id = 'the_capital_letter_n'; + c2.label = "NIIIIIIIIIIIIII"; + c2.style = 1; + m.custom_id = "temp"; m.title = "TITLE HERE"; - const a = new MessageActionRow(); - a.addComponent(c); - m.addComponent(a); + m.addComponent(c); + m.addComponent(c2); interaction.reply(m); return; @@ -37,7 +42,11 @@ export default async (interaction) => { // }); // const delResp = await interaction.client.commands.delete('none'); // console.log(delResp); - } else { + } + else if (interaction.type == interactionTypes.ModalSubmit) { + console.log(interaction.getComponents()); + } + else { console.log(interaction); } } \ No newline at end of file