Files
custom_discordjs/structures/interactions/Modal.js
T

121 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-04-25 20:33:28 -04:00
import { MessageActionRow } from "../messages/MessageActionRow.js";
2023-04-14 16:52:56 -04:00
import { Interaction } from "./interaction.js";
2023-04-25 17:34:01 -04:00
/** @enum {number} */
export const textInputStyle = Object.freeze({
Short: 1,
paragraph: 2
});
export class ModalComponent {
/** @type {String} */
custom_id;
/** @type {String} */
label;
/** @type {Boolean} */
required;
/** @type {number} */
min_length;
/** @type {number} */
max_length;
/** @type {textInputStyle} */
style;
/** @type {String} */
value;
/** @type {String} */
placeholder;
toObj() {
if (this.style != 1 && this.style != 2) throw `MODAL TEXT INPUT STYLE MUST BE 1 OR 2 BUT WAS '${this.style}'`;
const obj = {type: 4};
for (const k in this) {
if (this[k]) obj[k] = this[k];
}
return obj;
}
constructor(obj) {
2023-04-25 20:33:28 -04:00
if (obj == undefined) return;
2023-04-25 17:34:01 -04:00
for (const k in this) {
if (obj[k] != undefined) {
this[k] = obj[k];
}
}
}
}
2023-04-14 16:52:56 -04:00
export class Modal extends Interaction {
2023-04-25 17:34:01 -04:00
/** @type {String} */
title;
/** @type {String} */
custom_id;
2023-04-25 20:33:28 -04:00
/** @type {Map<String, ModalComponent>} */
2023-04-25 17:34:01 -04:00
components;
/**
2023-04-25 20:33:28 -04:00
* @param {ModalComponent} c
* @returns {Boolean} true is added false otherwise
2023-04-25 17:34:01 -04:00
*/
addComponent(c) {
2023-04-25 20:33:28 -04:00
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;
2023-04-25 17:34:01 -04:00
}
toObj() {
const obj = {title: this.title, custom_id: this.custom_id, components: []};
2023-04-25 20:33:28 -04:00
for (const key in this.components) {
const comp = this.components.get(key);
const a = new MessageActionRow();
a.addComponent(comp);
obj.components.push(a.toObj());
2023-04-25 17:34:01 -04:00
}
return obj;
}
2023-04-14 16:52:56 -04:00
constructor(intRaw, client) {
super(intRaw, client);
2023-04-25 20:33:28 -04:00
this.components = new Map();
2023-04-25 17:34:01 -04:00
if (!intRaw) return;
for (const opt of intRaw.data.components) {
2023-04-25 20:33:28 -04:00
const compRaw = opt.components[0];
const comp = new ModalComponent(compRaw);
this.components.set(comp.custom_id, comp.value);
2023-04-25 17:34:01 -04:00
}
2023-04-14 16:52:56 -04:00
}
}