feat(SelectMenu): allow emojis in options and option constructors (#7797)

This commit is contained in:
Rodry
2022-04-21 17:55:51 +01:00
committed by GitHub
parent 585169f2f0
commit f22245e9d0
3 changed files with 48 additions and 4 deletions

View File

@@ -5,25 +5,53 @@ const Transformers = require('../util/Transformers');
const Util = require('../util/Util'); const Util = require('../util/Util');
/** /**
* Represents a select menu builder. * Class used to build select menu components to be sent through the API
* @extends {BuildersSelectMenu} * @extends {BuildersSelectMenu}
*/ */
class SelectMenuBuilder extends BuildersSelectMenu { class SelectMenuBuilder extends BuildersSelectMenu {
constructor({ options, ...data } = {}) { constructor({ options, ...data } = {}) {
super( super(
Transformers.toSnakeCase({ Transformers.toSnakeCase({
...data,
options: options?.map(({ emoji, ...option }) => ({ options: options?.map(({ emoji, ...option }) => ({
...option, ...option,
emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji, emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji,
})), })),
...data,
}), }),
); );
} }
/** /**
* Creates a new select menu builder from JSON data * Adds options to this select menu
* @param {JSONEncodable<APISelectMenuComponent>|APISelectMenuComponent} other The other data * @param {APISelectMenuOption[]} options The options to add to this select menu
* @returns {SelectMenuBuilder}
*/
addOptions(...options) {
return super.addOptions(
options.map(({ emoji, ...option }) => ({
...option,
emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji,
})),
);
}
/**
* Sets the options on this select menu
* @param {APISelectMenuOption[]} options The options to set on this select menu
* @returns {SelectMenuBuilder}
*/
setOptions(...options) {
return super.setOptions(
options.map(({ emoji, ...option }) => ({
...option,
emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji,
})),
);
}
/**
* Creates a new select menu builder from json data
* @param {JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent} other The other data
* @returns {SelectMenuBuilder} * @returns {SelectMenuBuilder}
*/ */
static from(other) { static from(other) {

View File

@@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { SelectMenuOptionBuilder: BuildersSelectMenuOption } = require('@discordjs/builders'); const { SelectMenuOptionBuilder: BuildersSelectMenuOption } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');
const Util = require('../util/Util'); const Util = require('../util/Util');
/** /**
@@ -8,6 +9,14 @@ const Util = require('../util/Util');
* @extends {BuildersSelectMenuOption} * @extends {BuildersSelectMenuOption}
*/ */
class SelectMenuOptionBuilder extends BuildersSelectMenuOption { class SelectMenuOptionBuilder extends BuildersSelectMenuOption {
constructor({ emoji, ...data } = {}) {
super(
Transformers.toSnakeCase({
...data,
emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji,
}),
);
}
/** /**
* Sets the emoji to display on this option * Sets the emoji to display on this option
* @param {ComponentEmojiResolvable} emoji The emoji to display on this option * @param {ComponentEmojiResolvable} emoji The emoji to display on this option

View File

@@ -596,10 +596,17 @@ export class ButtonBuilder extends BuilderButtonComponent {
export class SelectMenuBuilder extends BuilderSelectMenuComponent { export class SelectMenuBuilder extends BuilderSelectMenuComponent {
public constructor(data?: Partial<SelectMenuComponentData | APISelectMenuComponent>); public constructor(data?: Partial<SelectMenuComponentData | APISelectMenuComponent>);
public override addOptions(
...options: (BuildersSelectMenuOption | SelectMenuComponentOptionData | APISelectMenuOption)[]
): this;
public override setOptions(
...options: (BuildersSelectMenuOption | SelectMenuComponentOptionData | APISelectMenuOption)[]
): this;
public static from(other: JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent): SelectMenuBuilder; public static from(other: JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent): SelectMenuBuilder;
} }
export class SelectMenuOptionBuilder extends BuildersSelectMenuOption { export class SelectMenuOptionBuilder extends BuildersSelectMenuOption {
public constructor(data?: SelectMenuComponentOptionData | APISelectMenuOption);
public setEmoji(emoji: ComponentEmojiResolvable): this; public setEmoji(emoji: ComponentEmojiResolvable): this;
} }