UNPKG

detritus-client

Version:

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

149 lines (148 loc) 5.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentSelectMenuOption = exports.ComponentSelectMenu = void 0; const baseset_1 = require("../../collections/baseset"); const constants_1 = require("../../constants"); const basestructure_1 = require("../../structures/basestructure"); const emoji_1 = require("../../structures/emoji"); const utils_1 = require("../../utils"); const actionbase_1 = require("./actionbase"); const keysComponentSelectMenu = new baseset_1.BaseSet([ constants_1.DiscordKeys.CUSTOM_ID, constants_1.DiscordKeys.MAX_VALUES, constants_1.DiscordKeys.MIN_VALUES, constants_1.DiscordKeys.OPTIONS, constants_1.DiscordKeys.PLACEHOLDER, constants_1.DiscordKeys.TYPE, ]); /** * Utils Component Select Menu Structure * @category Utils */ class ComponentSelectMenu extends actionbase_1.ComponentActionBase { constructor(data = {}) { super(data); this._keys = keysComponentSelectMenu; this.customId = ''; this.options = []; this.type = constants_1.MessageComponentTypes.SELECT_MENU; Object.assign(data, { [constants_1.DiscordKeys.CUSTOM_ID]: data[constants_1.DetritusKeys[constants_1.DiscordKeys.CUSTOM_ID]] || data[constants_1.DiscordKeys.CUSTOM_ID], [constants_1.DiscordKeys.MAX_VALUES]: data[constants_1.DetritusKeys[constants_1.DiscordKeys.MAX_VALUES]] || data[constants_1.DiscordKeys.MAX_VALUES], [constants_1.DiscordKeys.MIN_VALUES]: data[constants_1.DetritusKeys[constants_1.DiscordKeys.MIN_VALUES]] || data[constants_1.DiscordKeys.MIN_VALUES], }); this.merge(data); this.type = constants_1.MessageComponentTypes.SELECT_MENU; } addOption(option) { this.options.push(option); return this; } createOption(data = {}) { const option = new ComponentSelectMenuOption(data); this.addOption(option); return option; } setCustomId(customId) { this.merge({ custom_id: customId }); return this; } setMaxValues(maxValues) { this.merge({ max_values: maxValues }); return this; } setMinValues(minValues) { this.merge({ min_values: minValues }); return this; } setPlaceholder(placeholder) { this.merge({ placeholder }); return this; } mergeValue(key, value) { switch (key) { case constants_1.DiscordKeys.OPTIONS: { this.options.length = 0; for (let raw of value) { const option = new ComponentSelectMenuOption(raw); this.options.push(option); } } ; return; } return super.mergeValue(key, value); } } exports.ComponentSelectMenu = ComponentSelectMenu; const keysComponentSelectMenuOption = new baseset_1.BaseSet([ constants_1.DiscordKeys.DEFAULT, constants_1.DiscordKeys.DESCRIPTION, constants_1.DiscordKeys.EMOJI, constants_1.DiscordKeys.LABEL, constants_1.DiscordKeys.VALUE, ]); /** * Utils Component Select Menu Option Structure * @category Utils */ class ComponentSelectMenuOption extends basestructure_1.Structure { constructor(data = {}) { super(); this._keys = keysComponentSelectMenuOption; this.label = ''; this.value = ''; this.merge(data); } setDefault(isDefault) { this.merge({ default: isDefault }); return this; } setDescription(description) { this.merge({ description }); return this; } setEmoji(emoji) { this.merge({ emoji }); return this; } setLabel(label) { this.merge({ label }); return this; } setValue(value) { this.merge({ value }); return this; } mergeValue(key, value) { switch (key) { case constants_1.DiscordKeys.EMOJI: { if (value instanceof emoji_1.Emoji) { value = { animated: value.animated, id: value.id, name: value.name }; } else if (typeof (value) === 'string') { const { matches } = utils_1.regex(constants_1.DiscordRegexNames.EMOJI, value); if (matches.length) { value = matches[0]; } else { value = { name: value }; } } } ; break; } return super.mergeValue(key, value); } toJSON() { const data = super.toJSON(); if (data.emoji instanceof emoji_1.Emoji) { data.emoji = { animated: data.emoji.animated, id: data.emoji.id, name: data.emoji.name }; } return data; } } exports.ComponentSelectMenuOption = ComponentSelectMenuOption;