Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unsafe embed builder field normalization #7418

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/builders/src/components/Component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { APIMessageComponent, ComponentType } from 'discord-api-types/v9';
import type { JSONEncodable } from '../util/jsonEncodable';

/**
* Represents a discord component
*/
export interface Component {
export interface Component extends JSONEncodable<APIMessageComponent> {
/**
* The type of this component
*/
readonly type: ComponentType;
/**
* Converts this component to an API-compatible JSON object
*/
toJSON: () => APIMessageComponent;
}
2 changes: 2 additions & 0 deletions packages/builders/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export * from './interactions/slashCommands/options/user';

export * as ContextMenuCommandAssertions from './interactions/contextMenuCommands/Assertions';
export * from './interactions/contextMenuCommands/ContextMenuCommandBuilder';

export * from './util/jsonEncodable';
8 changes: 4 additions & 4 deletions packages/builders/src/messages/embed/UnsafeEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
APIEmbedThumbnail,
APIEmbedVideo,
} from 'discord-api-types/v9';
import { Embed } from './Embed';
import type { JSONEncodable } from '../../util/jsonEncodable';

export interface AuthorOptions {
name: string;
Expand All @@ -21,7 +21,7 @@ export interface FooterOptions {
iconURL?: string;
}

export class UnsafeEmbed implements APIEmbed {
export class UnsafeEmbed implements APIEmbed, JSONEncodable<APIEmbed> {
/**
* An array of fields of this embed
*/
Expand Down Expand Up @@ -126,7 +126,7 @@ export class UnsafeEmbed implements APIEmbed {
* @param fields The fields to add
*/
public addFields(...fields: APIEmbedField[]): this {
this.fields.push(...Embed.normalizeFields(...fields));
this.fields.push(...UnsafeEmbed.normalizeFields(...fields));
return this;
}

Expand All @@ -138,7 +138,7 @@ export class UnsafeEmbed implements APIEmbed {
* @param fields The replacing field objects
*/
public spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
this.fields.splice(index, deleteCount, ...Embed.normalizeFields(...fields));
this.fields.splice(index, deleteCount, ...UnsafeEmbed.normalizeFields(...fields));
return this;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/builders/src/util/jsonEncodable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface JSONEncodable<T> {
/**
* Transforms this object to its JSON format
*/
toJSON: () => T;
}

/**
* Indicates if an object is encodable or not.
* @param maybeEncodable The object to check against
*/
export function isJSONEncodable(maybeEncodable: unknown): maybeEncodable is JSONEncodable<unknown> {
return maybeEncodable !== null && typeof maybeEncodable === 'object' && 'toJSON' in maybeEncodable;
}