summaryrefslogtreecommitdiff
path: root/node_modules/discord.js/src/structures/StringSelectMenuBuilder.js
blob: ac555e745401e3833737d24b1a7c702ca9b5f40d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';

const { SelectMenuBuilder: BuildersSelectMenu, normalizeArray } = require('@discordjs/builders');
const { isJSONEncodable } = require('@discordjs/util');
const { toSnakeCase } = require('../util/Transformers');
const { resolvePartialEmoji } = require('../util/Util');

/**
 * Class used to build select menu components to be sent through the API
 * @extends {BuildersSelectMenu}
 */
class StringSelectMenuBuilder extends BuildersSelectMenu {
  constructor({ options, ...data } = {}) {
    super(
      toSnakeCase({
        ...data,
        options: options?.map(({ emoji, ...option }) => ({
          ...option,
          emoji: emoji && typeof emoji === 'string' ? resolvePartialEmoji(emoji) : emoji,
        })),
      }),
    );
  }

  /**
   * Normalizes a select menu option emoji
   * @param {SelectMenuOptionData|APISelectMenuOption} selectMenuOption The option to normalize
   * @returns {SelectMenuOptionBuilder|APISelectMenuOption}
   * @private
   */
  static normalizeEmoji(selectMenuOption) {
    if (isJSONEncodable(selectMenuOption)) {
      return selectMenuOption;
    }

    const { emoji, ...option } = selectMenuOption;
    return {
      ...option,
      emoji: typeof emoji === 'string' ? resolvePartialEmoji(emoji) : emoji,
    };
  }

  /**
   * Adds options to this select menu
   * @param {RestOrArray<APISelectMenuOption>} options The options to add to this select menu
   * @returns {StringSelectMenuBuilder}
   */
  addOptions(...options) {
    return super.addOptions(normalizeArray(options).map(option => StringSelectMenuBuilder.normalizeEmoji(option)));
  }

  /**
   * Sets the options on this select menu
   * @param {RestOrArray<APISelectMenuOption>} options The options to set on this select menu
   * @returns {StringSelectMenuBuilder}
   */
  setOptions(...options) {
    return super.setOptions(normalizeArray(options).map(option => StringSelectMenuBuilder.normalizeEmoji(option)));
  }

  /**
   * Creates a new select menu builder from json data
   * @param {StringSelectMenuBuilder|StringSelectMenuComponent|APIStringSelectComponent} other The other data
   * @returns {StringSelectMenuBuilder}
   */
  static from(other) {
    if (isJSONEncodable(other)) {
      return new this(other.toJSON());
    }
    return new this(other);
  }
}

module.exports = StringSelectMenuBuilder;

/**
 * @external BuildersSelectMenu
 * @see {@link https://discord.js.org/docs/packages/builders/stable/StringSelectMenuBuilder:Class}
 */