Skip to content

Commit

Permalink
feat(SelectMenu): allow emojis in options and option constructors (#7797
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ImRodry committed Apr 21, 2022
1 parent 585169f commit f22245e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
36 changes: 32 additions & 4 deletions packages/discord.js/src/structures/SelectMenuBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,53 @@ const Transformers = require('../util/Transformers');
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}
*/
class SelectMenuBuilder extends BuildersSelectMenu {
constructor({ options, ...data } = {}) {
super(
Transformers.toSnakeCase({
...data,
options: options?.map(({ emoji, ...option }) => ({
...option,
emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji,
})),
...data,
}),
);
}

/**
* Creates a new select menu builder from JSON data
* @param {JSONEncodable<APISelectMenuComponent>|APISelectMenuComponent} other The other data
* Adds options to this select menu
* @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}
*/
static from(other) {
Expand Down
9 changes: 9 additions & 0 deletions packages/discord.js/src/structures/SelectMenuOptionBuilder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
'use strict';

const { SelectMenuOptionBuilder: BuildersSelectMenuOption } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');
const Util = require('../util/Util');

/**
* Represents a select menu option builder.
* @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
* @param {ComponentEmojiResolvable} emoji The emoji to display on this option
Expand Down
7 changes: 7 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,17 @@ export class ButtonBuilder extends BuilderButtonComponent {

export class SelectMenuBuilder extends BuilderSelectMenuComponent {
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;
}

export class SelectMenuOptionBuilder extends BuildersSelectMenuOption {
public constructor(data?: SelectMenuComponentOptionData | APISelectMenuOption);
public setEmoji(emoji: ComponentEmojiResolvable): this;
}

Expand Down

0 comments on commit f22245e

Please sign in to comment.