/
SelectMenuBuilder.js
78 lines (69 loc) · 2.34 KB
/
SelectMenuBuilder.js
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
'use strict';
const { SelectMenuBuilder: BuildersSelectMenu, isJSONEncodable, normalizeArray } = require('@discordjs/builders');
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 SelectMenuBuilder 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|JSONEncodable<APISelectMenuOption>} selectMenuOption The option to normalize
* @returns {Array<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 {SelectMenuBuilder}
*/
addOptions(...options) {
return super.addOptions(normalizeArray(options).map(option => SelectMenuBuilder.normalizeEmoji(option)));
}
/**
* Sets the options on this select menu
* @param {RestOrArray<APISelectMenuOption>} options The options to set on this select menu
* @returns {SelectMenuBuilder}
*/
setOptions(...options) {
return super.setOptions(normalizeArray(options).map(option => SelectMenuBuilder.normalizeEmoji(option)));
}
/**
* Creates a new select menu builder from json data
* @param {JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent} other The other data
* @returns {SelectMenuBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
}
}
module.exports = SelectMenuBuilder;
/**
* @external BuildersSelectMenu
* @see {@link https://discord.js.org/#/docs/builders/main/class/SelectMenuBuilder}
*/