Skip to content

Commit

Permalink
feat: add #from for easy builder conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Mar 10, 2022
1 parent 21966b8 commit cbd057a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
14 changes: 13 additions & 1 deletion packages/discord.js/src/structures/ButtonBuilder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
'use strict';

const { ButtonBuilder: BuildersButtonComponent } = require('@discordjs/builders');
const { ButtonBuilder: BuildersButtonComponent, isJSONEncodable } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');

class ButtonBuilder extends BuildersButtonComponent {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* Creates a new button builder from json data
* @param {JSONEncodable<APIButtonComponent> | APIButtonComponent} other The other data
* @returns {ButtonBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
}
}

module.exports = ButtonBuilder;
14 changes: 13 additions & 1 deletion packages/discord.js/src/structures/EmbedBuilder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
'use strict';

const { EmbedBuilder: BuildersEmbed } = require('@discordjs/builders');
const { EmbedBuilder: BuildersEmbed, isJSONEncodable } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');

class EmbedBuilder extends BuildersEmbed {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* Creates a new embed builder from json data
* @param {JSONEncodable<APIEmbed> | APIEmbed} other The other data
* @returns {EmbedBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
}
}

module.exports = EmbedBuilder;
14 changes: 13 additions & 1 deletion packages/discord.js/src/structures/SelectMenuBuilder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
'use strict';

const { SelectMenuBuilder: BuildersSelectMenuComponent } = require('@discordjs/builders');
const { SelectMenuBuilder: BuildersSelectMenuComponent, isJSONEncodable } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');

class SelectMenuBuilder extends BuildersSelectMenuComponent {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* 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;
14 changes: 13 additions & 1 deletion packages/discord.js/src/structures/TextInputBuilder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
'use strict';

const { TextInputBuilder: BuildersTextInputComponent } = require('@discordjs/builders');
const { TextInputBuilder: BuildersTextInputComponent, isJSONEncodable } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');

class TextInputBuilder extends BuildersTextInputComponent {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* Creates a new text input builder from json data
* @param {JSONEncodable<APITextInputComponent> | APITextInputComponent} other The other data
* @returns {TextInputBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
}
}

module.exports = TextInputBuilder;
5 changes: 4 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ButtonBuilder as BuilderButtonComponent,
channelMention,
codeBlock,
ComponentBuilder,
EmbedBuilder as BuildersEmbed,
formatEmoji,
hideLinkEmbed,
Expand Down Expand Up @@ -541,16 +540,19 @@ export class ButtonComponent extends Component<APIButtonComponent> {

export class ButtonBuilder extends BuilderButtonComponent {
public constructor(data?: ButtonComponentData | (Omit<APIButtonComponent, 'type'> & { type?: ComponentType.Button }));
public static from(other: JSONEncodable<APIButtonComponent> | APIButtonComponent): ButtonBuilder;
}

export class SelectMenuBuilder extends BuilderSelectMenuComponent {
public constructor(
data?: SelectMenuComponentData | (Omit<APISelectMenuComponent, 'type'> & { type?: ComponentType.SelectMenu }),
);
public static from(other: JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent): SelectMenuBuilder;
}

export class TextInputBuilder extends BuilderTextInputComponent {
public constructor(data?: TextInputComponentData | APITextInputComponent);
public static from(other: JSONEncodable<APITextInputComponent> | APITextInputComponent): TextInputBuilder;
}

export class TextInputComponent extends Component<APITextInputComponent> {
Expand Down Expand Up @@ -604,6 +606,7 @@ export interface EmbedImageData extends Omit<APIEmbedImage, 'proxy_url'> {
export class EmbedBuilder extends BuildersEmbed {
public constructor(data?: EmbedData | APIEmbed);
public override setColor(color: ColorResolvable | null): this;
public static from(other: JSONEncodable<APIEmbed> | APIEmbed): EmbedBuilder;
}

export class Embed {
Expand Down
29 changes: 29 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
AuditLogEvent,
ButtonStyle,
TextInputStyle,
APITextInputComponent,
APIEmbed,
} from 'discord-api-types/v9';
import {
ApplicationCommand,
Expand Down Expand Up @@ -113,6 +115,9 @@ import {
EmbedBuilder,
MessageActionRowComponent,
SelectMenuBuilder,
TextInputBuilder,
TextInputComponent,
Embed,
} from '.';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';

Expand Down Expand Up @@ -1404,3 +1409,27 @@ chatInputInteraction.showModal({
},
],
});

declare const selectMenuData: APISelectMenuComponent;
SelectMenuBuilder.from(selectMenuData);

declare const selectMenuComp: SelectMenuComponent;
SelectMenuBuilder.from(selectMenuComp);

declare const buttonData: APIButtonComponent;
ButtonBuilder.from(buttonData);

declare const buttonComp: ButtonComponent;
ButtonBuilder.from(buttonComp);

declare const textInputData: APITextInputComponent;
TextInputBuilder.from(textInputData);

declare const textInputComp: TextInputComponent;
TextInputBuilder.from(textInputComp);

declare const embedData: APIEmbed;
EmbedBuilder.from(embedData);

declare const embedComp: Embed;
EmbedBuilder.from(embedComp);

0 comments on commit cbd057a

Please sign in to comment.