mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
refactor: Don't return builders from API data (#7584)
* refactor: don't return builders from API data * Update packages/discord.js/src/structures/ActionRow.js Co-authored-by: Antonio Román <kyradiscord@gmail.com> * fix: circular dependency * fix: circular dependency pt.2 * chore: make requested changes * chore: bump dapi-types * chore: convert text input * chore: convert text input * feat: handle cases of unknown component types better * refactor: refactor modal to builder * feat: add #from for easy builder conversions * refactor: make requested changes * chore: make requested changes * style: fix linting error Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: almeidx <almeidx@pm.me>
This commit is contained in:
@@ -10,15 +10,15 @@ import {
|
||||
urlPredicate,
|
||||
validateFieldLength,
|
||||
} from './Assertions';
|
||||
import { EmbedAuthorOptions, EmbedFooterOptions, RGBTuple, UnsafeEmbed } from './UnsafeEmbed';
|
||||
import { EmbedAuthorOptions, EmbedFooterOptions, RGBTuple, UnsafeEmbedBuilder } from './UnsafeEmbed';
|
||||
|
||||
/**
|
||||
* Represents a validated embed in a message (image/video preview, rich embed, etc.)
|
||||
*/
|
||||
export class Embed extends UnsafeEmbed {
|
||||
export class EmbedBuilder extends UnsafeEmbedBuilder {
|
||||
public override addFields(...fields: APIEmbedField[]): this {
|
||||
// Ensure adding these fields won't exceed the 25 field limit
|
||||
validateFieldLength(fields.length, this.fields);
|
||||
validateFieldLength(fields.length, this.data.fields);
|
||||
|
||||
// Data assertions
|
||||
return super.addFields(...embedFieldsArrayPredicate.parse(fields));
|
||||
@@ -26,7 +26,7 @@ export class Embed extends UnsafeEmbed {
|
||||
|
||||
public override spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
|
||||
// Ensure adding these fields won't exceed the 25 field limit
|
||||
validateFieldLength(fields.length - deleteCount, this.fields);
|
||||
validateFieldLength(fields.length - deleteCount, this.data.fields);
|
||||
|
||||
// Data assertions
|
||||
return super.spliceFields(index, deleteCount, ...embedFieldsArrayPredicate.parse(fields));
|
||||
|
||||
@@ -1,13 +1,4 @@
|
||||
import type {
|
||||
APIEmbed,
|
||||
APIEmbedAuthor,
|
||||
APIEmbedField,
|
||||
APIEmbedFooter,
|
||||
APIEmbedImage,
|
||||
APIEmbedVideo,
|
||||
} from 'discord-api-types/v9';
|
||||
import type { Equatable } from '../../util/equatable';
|
||||
import isEqual from 'fast-deep-equal';
|
||||
import type { APIEmbed, APIEmbedAuthor, APIEmbedField, APIEmbedFooter, APIEmbedImage } from 'discord-api-types/v9';
|
||||
|
||||
export type RGBTuple = [red: number, green: number, blue: number];
|
||||
|
||||
@@ -40,7 +31,7 @@ export interface EmbedImageData extends Omit<APIEmbedImage, 'proxy_url'> {
|
||||
/**
|
||||
* Represents a non-validated embed in a message (image/video preview, rich embed, etc.)
|
||||
*/
|
||||
export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
|
||||
export class UnsafeEmbedBuilder {
|
||||
public readonly data: APIEmbed;
|
||||
|
||||
public constructor(data: APIEmbed = {}) {
|
||||
@@ -48,133 +39,6 @@ export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
|
||||
if (data.timestamp) this.data.timestamp = new Date(data.timestamp).toISOString();
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of fields of this embed
|
||||
*/
|
||||
public get fields() {
|
||||
return this.data.fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed title
|
||||
*/
|
||||
public get title() {
|
||||
return this.data.title;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed description
|
||||
*/
|
||||
public get description() {
|
||||
return this.data.description;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed URL
|
||||
*/
|
||||
public get url() {
|
||||
return this.data.url;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed color
|
||||
*/
|
||||
public get color() {
|
||||
return this.data.color;
|
||||
}
|
||||
|
||||
/**
|
||||
* The timestamp of the embed in an ISO 8601 format
|
||||
*/
|
||||
public get timestamp() {
|
||||
return this.data.timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed thumbnail data
|
||||
*/
|
||||
public get thumbnail(): EmbedImageData | undefined {
|
||||
if (!this.data.thumbnail) return undefined;
|
||||
return {
|
||||
url: this.data.thumbnail.url,
|
||||
proxyURL: this.data.thumbnail.proxy_url,
|
||||
height: this.data.thumbnail.height,
|
||||
width: this.data.thumbnail.width,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed image data
|
||||
*/
|
||||
public get image(): EmbedImageData | undefined {
|
||||
if (!this.data.image) return undefined;
|
||||
return {
|
||||
url: this.data.image.url,
|
||||
proxyURL: this.data.image.proxy_url,
|
||||
height: this.data.image.height,
|
||||
width: this.data.image.width,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Received video data
|
||||
*/
|
||||
public get video(): APIEmbedVideo | undefined {
|
||||
return this.data.video;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed author data
|
||||
*/
|
||||
public get author(): EmbedAuthorData | undefined {
|
||||
if (!this.data.author) return undefined;
|
||||
return {
|
||||
name: this.data.author.name,
|
||||
url: this.data.author.url,
|
||||
iconURL: this.data.author.icon_url,
|
||||
proxyIconURL: this.data.author.proxy_icon_url,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Received data about the embed provider
|
||||
*/
|
||||
public get provider() {
|
||||
return this.data.provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed footer data
|
||||
*/
|
||||
public get footer(): EmbedFooterData | undefined {
|
||||
if (!this.data.footer) return undefined;
|
||||
return {
|
||||
text: this.data.footer.text,
|
||||
iconURL: this.data.footer.icon_url,
|
||||
proxyIconURL: this.data.footer.proxy_icon_url,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The accumulated length for the embed title, description, fields, footer text, and author name
|
||||
*/
|
||||
public get length(): number {
|
||||
return (
|
||||
(this.data.title?.length ?? 0) +
|
||||
(this.data.description?.length ?? 0) +
|
||||
(this.data.fields?.reduce((prev, curr) => prev + curr.name.length + curr.value.length, 0) ?? 0) +
|
||||
(this.data.footer?.text.length ?? 0) +
|
||||
(this.data.author?.name.length ?? 0)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The hex color of the current color of the embed
|
||||
*/
|
||||
public get hexColor() {
|
||||
return typeof this.data.color === 'number' ? `#${this.data.color.toString(16).padStart(6, '0')}` : this.data.color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds fields to the embed (max 25)
|
||||
*
|
||||
@@ -204,7 +68,7 @@ export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
|
||||
* @param fields The fields to set
|
||||
*/
|
||||
public setFields(...fields: APIEmbedField[]) {
|
||||
this.spliceFields(0, this.fields?.length ?? 0, ...fields);
|
||||
this.spliceFields(0, this.data.fields?.length ?? 0, ...fields);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -319,11 +183,4 @@ export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
|
||||
public toJSON(): APIEmbed {
|
||||
return { ...this.data };
|
||||
}
|
||||
|
||||
public equals(other: UnsafeEmbed | APIEmbed) {
|
||||
const { image: thisImage, thumbnail: thisThumbnail, ...thisData } = this.data;
|
||||
const data = other instanceof UnsafeEmbed ? other.data : other;
|
||||
const { image, thumbnail, ...otherData } = data;
|
||||
return isEqual(otherData, thisData) && image?.url === thisImage?.url && thumbnail?.url === thisThumbnail?.url;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user