From ad36c0be7744ea4214ccf345fe80a5a1a9e89101 Mon Sep 17 00:00:00 2001 From: Parbez Date: Sun, 5 Jun 2022 22:03:19 +0530 Subject: [PATCH] fix: add static method `from` in builders (#7990) --- .../discord.js/src/structures/ActionRowBuilder.js | 15 ++++++++++++++- .../src/structures/SelectMenuOptionBuilder.js | 14 +++++++++++++- packages/discord.js/typings/index.d.ts | 3 ++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/structures/ActionRowBuilder.js b/packages/discord.js/src/structures/ActionRowBuilder.js index bd3a78352..871d51953 100644 --- a/packages/discord.js/src/structures/ActionRowBuilder.js +++ b/packages/discord.js/src/structures/ActionRowBuilder.js @@ -1,6 +1,6 @@ 'use strict'; -const { ActionRowBuilder: BuildersActionRow, ComponentBuilder } = require('@discordjs/builders'); +const { ActionRowBuilder: BuildersActionRow, ComponentBuilder, isJSONEncodable } = require('@discordjs/builders'); const Components = require('../util/Components'); const Transformers = require('../util/Transformers'); @@ -15,6 +15,19 @@ class ActionRowBuilder extends BuildersActionRow { components: components?.map(c => (c instanceof ComponentBuilder ? c : Components.createComponentBuilder(c))), }); } + + /** + * Creates a new action row builder from JSON data + * @param {JSONEncodable> + * |APIActionRowComponent} other The other data + * @returns {ActionRowBuilder} + */ + static from(other) { + if (isJSONEncodable(other)) { + return new this(other.toJSON()); + } + return new this(other); + } } module.exports = ActionRowBuilder; diff --git a/packages/discord.js/src/structures/SelectMenuOptionBuilder.js b/packages/discord.js/src/structures/SelectMenuOptionBuilder.js index ea816bea2..981b8d0ec 100644 --- a/packages/discord.js/src/structures/SelectMenuOptionBuilder.js +++ b/packages/discord.js/src/structures/SelectMenuOptionBuilder.js @@ -1,6 +1,6 @@ 'use strict'; -const { SelectMenuOptionBuilder: BuildersSelectMenuOption } = require('@discordjs/builders'); +const { SelectMenuOptionBuilder: BuildersSelectMenuOption, isJSONEncodable } = require('@discordjs/builders'); const Transformers = require('../util/Transformers'); const Util = require('../util/Util'); @@ -28,6 +28,18 @@ class SelectMenuOptionBuilder extends BuildersSelectMenuOption { } return super.setEmoji(emoji); } + + /** + * Creates a new select menu option builder from JSON data + * @param {JSONEncodable|APISelectMenuOption} other The other data + * @returns {SelectMenuOptionBuilder} + */ + static from(other) { + if (isJSONEncodable(other)) { + return new this(other.toJSON()); + } + return new this(other); + } } module.exports = SelectMenuOptionBuilder; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 509473eb8..acb4d73a2 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -610,7 +610,8 @@ export class SelectMenuBuilder extends BuilderSelectMenuComponent { export class SelectMenuOptionBuilder extends BuildersSelectMenuOption { public constructor(data?: SelectMenuComponentOptionData | APISelectMenuOption); - public setEmoji(emoji: ComponentEmojiResolvable): this; + public override setEmoji(emoji: ComponentEmojiResolvable): this; + public static from(other: JSONEncodable | APISelectMenuOption): SelectMenuOptionBuilder; } export class ModalBuilder extends BuildersModal {