From b936103395121cb21a8c616f669ddab1d2efb0f1 Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Mon, 7 Feb 2022 05:52:10 -0500 Subject: [PATCH] fix: unsafe embed builder field normalization (#7418) --- packages/builders/src/components/Component.ts | 7 ++----- packages/builders/src/index.ts | 2 ++ .../builders/src/messages/embed/UnsafeEmbed.ts | 8 ++++---- packages/builders/src/util/jsonEncodable.ts | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 packages/builders/src/util/jsonEncodable.ts diff --git a/packages/builders/src/components/Component.ts b/packages/builders/src/components/Component.ts index eaa60a501d8d..992c9199d445 100644 --- a/packages/builders/src/components/Component.ts +++ b/packages/builders/src/components/Component.ts @@ -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 { /** * The type of this component */ readonly type: ComponentType; - /** - * Converts this component to an API-compatible JSON object - */ - toJSON: () => APIMessageComponent; } diff --git a/packages/builders/src/index.ts b/packages/builders/src/index.ts index 8cd32e58931f..228a4ea4b99e 100644 --- a/packages/builders/src/index.ts +++ b/packages/builders/src/index.ts @@ -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'; diff --git a/packages/builders/src/messages/embed/UnsafeEmbed.ts b/packages/builders/src/messages/embed/UnsafeEmbed.ts index 6b76573981ef..b286e6168d0b 100644 --- a/packages/builders/src/messages/embed/UnsafeEmbed.ts +++ b/packages/builders/src/messages/embed/UnsafeEmbed.ts @@ -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; @@ -21,7 +21,7 @@ export interface FooterOptions { iconURL?: string; } -export class UnsafeEmbed implements APIEmbed { +export class UnsafeEmbed implements APIEmbed, JSONEncodable { /** * An array of fields of this embed */ @@ -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; } @@ -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; } diff --git a/packages/builders/src/util/jsonEncodable.ts b/packages/builders/src/util/jsonEncodable.ts new file mode 100644 index 000000000000..78f422dd78cd --- /dev/null +++ b/packages/builders/src/util/jsonEncodable.ts @@ -0,0 +1,14 @@ +export interface JSONEncodable { + /** + * 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 { + return maybeEncodable !== null && typeof maybeEncodable === 'object' && 'toJSON' in maybeEncodable; +}