Skip to content

Commit

Permalink
refactor: Tidy up builders and components (#7711)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Apr 14, 2022
1 parent 01a423d commit 96a0d83
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 63 deletions.
4 changes: 2 additions & 2 deletions packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ exports.ContextMenuCommandInteraction = require('./structures/ContextMenuCommand
exports.DMChannel = require('./structures/DMChannel');
exports.Embed = require('./structures/Embed');
exports.EmbedBuilder = require('./structures/EmbedBuilder');
exports.UnsafeEmbed = require('@discordjs/builders').UnsafeEmbed;
exports.Emoji = require('./structures/Emoji').Emoji;
exports.Guild = require('./structures/Guild').Guild;
exports.GuildAuditLogs = require('./structures/GuildAuditLogs');
Expand All @@ -122,6 +121,7 @@ exports.InviteStageInstance = require('./structures/InviteStageInstance');
exports.InviteGuild = require('./structures/InviteGuild');
exports.Message = require('./structures/Message').Message;
exports.MessageAttachment = require('./structures/MessageAttachment');
exports.ModalBuilder = require('./structures/ModalBuilder');
exports.MessageCollector = require('./structures/MessageCollector');
exports.MessageComponentInteraction = require('./structures/MessageComponentInteraction');
exports.MessageContextMenuCommandInteraction = require('./structures/MessageContextMenuCommandInteraction');
Expand Down Expand Up @@ -196,7 +196,6 @@ exports.InviteTargetType = require('discord-api-types/v10').InviteTargetType;
exports.Locale = require('discord-api-types/v10').Locale;
exports.MessageType = require('discord-api-types/v10').MessageType;
exports.MessageFlags = require('discord-api-types/v10').MessageFlags;
exports.ModalBuilder = require('@discordjs/builders').ModalBuilder;
exports.OAuth2Scopes = require('discord-api-types/v10').OAuth2Scopes;
exports.PermissionFlagsBits = require('discord-api-types/v10').PermissionFlagsBits;
exports.RESTJSONErrorCodes = require('discord-api-types/v10').RESTJSONErrorCodes;
Expand All @@ -207,6 +206,7 @@ exports.TextInputStyle = require('discord-api-types/v10').TextInputStyle;
exports.UserFlags = require('discord-api-types/v10').UserFlags;
exports.WebhookType = require('discord-api-types/v10').WebhookType;
exports.UnsafeButtonBuilder = require('@discordjs/builders').UnsafeButtonBuilder;
exports.UnsafeEmbedBuilder = require('@discordjs/builders').UnsafeEmbedBuilder;
exports.UnsafeSelectMenuBuilder = require('@discordjs/builders').UnsafeSelectMenuBuilder;
exports.UnsafeSelectMenuOptionBuilder = require('@discordjs/builders').UnsafeSelectMenuOptionBuilder;
exports.UnsafeModalBuilder = require('@discordjs/builders').UnsafeModalBuilder;
Expand Down
14 changes: 14 additions & 0 deletions packages/discord.js/src/structures/ActionRow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { isJSONEncodable } = require('@discordjs/builders');
const Component = require('./Component');
const Components = require('../util/Components');

Expand All @@ -10,6 +11,7 @@ const Components = require('../util/Components');
class ActionRow extends Component {
constructor({ components, ...data }) {
super(data);

/**
* The components in this action row
* @type {Component[]}
Expand All @@ -18,6 +20,18 @@ class ActionRow extends Component {
this.components = components.map(c => Components.createComponent(c));
}

/**
* Creates a new action row builder from JSON data
* @param {JSONEncodable<APIActionRowComponent>|APIActionRowComponent} other The other data
* @returns {ActionRowBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
}

/**
* Returns the API-compatible JSON for this component
* @returns {APIActionRowComponent}
Expand Down
11 changes: 10 additions & 1 deletion packages/discord.js/src/structures/ActionRowBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
const { ActionRowBuilder: BuildersActionRow, ComponentBuilder } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');

/**
* Represents an action row builder.
* @extends {BuildersActionRow}
*/
class ActionRowBuilder extends BuildersActionRow {
constructor({ components, ...data } = {}) {
super({
components: components?.map(c => (c instanceof ComponentBuilder ? c : Transformers.toSnakeCase(c))),
...Transformers.toSnakeCase(data),
components: components?.map(c => (c instanceof ComponentBuilder ? c : Transformers.toSnakeCase(c))),
});
}
}

module.exports = ActionRowBuilder;

/**
* @external BuildersActionRow
* @see {@link https://discord.js.org/#/docs/builders/main/class/ActionRowBuilder}
*/
19 changes: 14 additions & 5 deletions packages/discord.js/src/structures/ButtonBuilder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

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

class ButtonBuilder extends BuildersButtonComponent {
/**
* Represents a button builder.
* @extends {BuildersButton}
*/
class ButtonBuilder extends BuildersButton {
constructor({ emoji, ...data }) {
super(
Transformers.toSnakeCase({ ...data, emoji: emoji && typeof emoji === 'string' ? Util.parseEmoji(emoji) : emoji }),
Expand All @@ -14,7 +18,7 @@ class ButtonBuilder extends BuildersButtonComponent {
/**
* Sets the emoji to display on this button
* @param {string|APIMessageComponentEmoji} emoji The emoji to display on this button
* @returns {ButtonComponent}
* @returns {ButtonBuilder}
*/
setEmoji(emoji) {
if (typeof emoji === 'string') {
Expand All @@ -24,8 +28,8 @@ class ButtonBuilder extends BuildersButtonComponent {
}

/**
* Creates a new button builder from json data
* @param {JSONEncodable<APIButtonComponent> | APIButtonComponent} other The other data
* Creates a new button builder from JSON data
* @param {JSONEncodable<APIButtonComponent>|APIButtonComponent} other The other data
* @returns {ButtonBuilder}
*/
static from(other) {
Expand All @@ -37,3 +41,8 @@ class ButtonBuilder extends BuildersButtonComponent {
}

module.exports = ButtonBuilder;

/**
* @external BuildersButton
* @see {@link https://discord.js.org/#/docs/builders/main/class/ButtonBuilder}
*/
3 changes: 3 additions & 0 deletions packages/discord.js/src/structures/Embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const isEqual = require('fast-deep-equal');

/**
* Represents an embed.
*/
class Embed {
/**
* Creates a new embed object
Expand Down
34 changes: 20 additions & 14 deletions packages/discord.js/src/structures/EmbedBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,40 @@ const { EmbedBuilder: BuildersEmbed, isJSONEncodable } = require('@discordjs/bui
const Transformers = require('../util/Transformers');
const Util = require('../util/Util');

/**
* Represents an embed builder.
* @extends {BuildersEmbed}
*/
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
* Sets the color of this embed
* @param {?ColorResolvable} color The color of the embed
* @returns {EmbedBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
setColor(color) {
return super.setColor(color && Util.resolveColor(color));
}

/**
* Sets the color of this embed
* @param {?ColorResolvable} color The color of the embed
* @returns {Embed}
* Creates a new embed builder from JSON data
* @param {JSONEncodable<APIEmbed>|APIEmbed} other The other data
* @returns {EmbedBuilder}
*/
setColor(color) {
if (color === null) {
return super.setColor(null);
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return super.setColor(Util.resolveColor(color));
return new this(other);
}
}

module.exports = EmbedBuilder;

/**
* @external BuildersEmbed
* @see {@link https://discord.js.org/#/docs/builders/main/class/EmbedBuilder}
*/
25 changes: 0 additions & 25 deletions packages/discord.js/src/structures/MessageComponentInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,3 @@ module.exports = MessageComponentInteraction;
* @external APIMessageButton
* @see {@link https://discord.com/developers/docs/interactions/message-components#button-object}
*/

/**
* @external ButtonComponent
* @see {@link https://discord.js.org/#/docs/builders/main/class/ButtonComponent}
*/

/**
* @external SelectMenuComponent
* @see {@link https://discord.js.org/#/docs/builders/main/class/SelectMenuComponent}
*/

/**
* @external SelectMenuOption
* @see {@link https://discord.js.org/#/docs/builders/main/class/SelectMenuComponent}
*/

/**
* @external ActionRow
* @see {@link https://discord.js.org/#/docs/builders/main/class/ActionRow}
*/

/**
* @external Embed
* @see {@link https://discord.js.org/#/docs/builders/main/class/Embed}
*/
36 changes: 36 additions & 0 deletions packages/discord.js/src/structures/ModalBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const { ModalBuilder: BuildersModal, ComponentBuilder, isJSONEncodable } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');

/**
* Represents a modal builder.
* @extends {BuildersModal}
*/
class ModalBuilder extends BuildersModal {
constructor({ components, ...data }) {
super({
...Transformers.toSnakeCase(data),
components: components?.map(c => (c instanceof ComponentBuilder ? c : Transformers.toSnakeCase(c))),
});
}

/**
* Creates a new modal builder from JSON data
* @param {JSONEncodable<APIModalComponent>|APIModalComponent} other The other data
* @returns {ModalBuilder}
*/
static from(other) {
if (isJSONEncodable(other)) {
return new this(other.toJSON());
}
return new this(other);
}
}

module.exports = ModalBuilder;

/**
* @external BuildersModal
* @see {@link https://discord.js.org/#/docs/builders/main/class/ModalBuilder}
*/
17 changes: 13 additions & 4 deletions packages/discord.js/src/structures/SelectMenuBuilder.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

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

class SelectMenuBuilder extends BuildersSelectMenuComponent {
/**
* Represents a select menu builder.
* @extends {BuildersSelectMenu}
*/
class SelectMenuBuilder extends BuildersSelectMenu {
constructor({ options, ...data }) {
super(
Transformers.toSnakeCase({
Expand All @@ -18,8 +22,8 @@ class SelectMenuBuilder extends BuildersSelectMenuComponent {
}

/**
* Creates a new select menu builder from json data
* @param {JSONEncodable<APISelectMenuComponent> | APISelectMenuComponent} other The other data
* Creates a new select menu builder from JSON data
* @param {JSONEncodable<APISelectMenuComponent>|APISelectMenuComponent} other The other data
* @returns {SelectMenuBuilder}
*/
static from(other) {
Expand All @@ -31,3 +35,8 @@ class SelectMenuBuilder extends BuildersSelectMenuComponent {
}

module.exports = SelectMenuBuilder;

/**
* @external BuildersSelectMenu
* @see {@link https://discord.js.org/#/docs/builders/main/class/SelectMenuBuilder}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { SelectMenuOptionBuilder: BuildersSelectMenuOption } = require('@discordj
const Util = require('../util/Util');

/**
* Represents a select menu option
* Represents a select menu option builder.
* @extends {BuildersSelectMenuOption}
*/
class SelectMenuOptionBuilder extends BuildersSelectMenuOption {
/**
Expand All @@ -21,3 +22,8 @@ class SelectMenuOptionBuilder extends BuildersSelectMenuOption {
}

module.exports = SelectMenuOptionBuilder;

/**
* @external BuildersSelectMenuOption
* @see {@link https://discord.js.org/#/docs/builders/main/class/SelectMenuOptionBuilder}
*/
17 changes: 13 additions & 4 deletions packages/discord.js/src/structures/TextInputBuilder.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
'use strict';

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

class TextInputBuilder extends BuildersTextInputComponent {
/**
* Represents a text input builder.
* @extends {BuildersTextInput}
*/
class TextInputBuilder extends BuildersTextInput {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* Creates a new text input builder from json data
* @param {JSONEncodable<APITextInputComponent> | APITextInputComponent} other The other data
* Creates a new text input builder from JSON data
* @param {JSONEncodable<APITextInputComponent>|APITextInputComponent} other The other data
* @returns {TextInputBuilder}
*/
static from(other) {
Expand All @@ -22,3 +26,8 @@ class TextInputBuilder extends BuildersTextInputComponent {
}

module.exports = TextInputBuilder;

/**
* @external BuildersTextInput
* @see {@link https://discord.js.org/#/docs/builders/main/class/TextInputBuilder}
*/
8 changes: 8 additions & 0 deletions packages/discord.js/src/structures/TextInputComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

const Component = require('./Component');

/**
* Represents a text input component.
* @extends {Component}
*/
class TextInputComponent extends Component {
/**
* The custom id of this text input
* @type {string}
* @readonly
*/
get customId() {
return this.data.custom_id;
}

/**
* The value for this text input
* @type {string}
* @readonly
*/
get value() {
return this.data.value;
Expand Down
Loading

0 comments on commit 96a0d83

Please sign in to comment.