From aac841101a4980cda8547bf83475c078246ff541 Mon Sep 17 00:00:00 2001 From: waterflamev8 Date: Sat, 20 Mar 2021 13:21:35 +0800 Subject: [PATCH 1/2] Add embed validation --- src/structures/embed.ts | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/structures/embed.ts b/src/structures/embed.ts index b2d8053..1303f58 100644 --- a/src/structures/embed.ts +++ b/src/structures/embed.ts @@ -29,6 +29,15 @@ export class Embed { fields?: EmbedField[] files: MessageAttachment[] = [] + static MAX_TITLE_LENGTH = 256 + static MAX_DESCRIPTION_LENGTH = 2048 + static MAX_FIELD_NAME_LENGTH = 256 + static MAX_FIELD_VALUE_LENGTH = 1024 + static MAX_FIELDS_LENGTH = 25 + static MAX_FOOTER_TEXT_LENGTH = 2048 + static MAX_AUTHOR_NAME_LENGTH = 256 + static MAX_EMBED_LENGTH = 6000 + constructor(data?: EmbedPayload) { this.title = data?.title this.type = data?.type @@ -47,6 +56,44 @@ export class Embed { /** Convert Embed Object to Embed Payload JSON */ toJSON(): EmbedPayload { + let total = 0; + if (this.title?.length !== undefined && this.title?.length > Embed.MAX_TITLE_LENGTH) { + total += this.title.length + throw new Error(`Embed title cannot exceed ${Embed.MAX_TITLE_LENGTH} characters.`) + } + + if (this.description?.length !== undefined && this.description?.length > Embed.MAX_DESCRIPTION_LENGTH) { + total += this.description.length + throw new Error(`Embed description cannot exceed ${Embed.MAX_DESCRIPTION_LENGTH} characters.`) + } + + if (this.fields?.length !== undefined) { + this.fields.forEach((field) => { + if (field.name.length > Embed.MAX_FIELD_NAME_LENGTH) { + total += field.name.length + throw new Error(`Embed field name cannot exceed ${Embed.MAX_FIELD_NAME_LENGTH} characters.`) + } + + if (field.value.length > Embed.MAX_FIELD_VALUE_LENGTH) { + total += field.value.length + throw new Error(`Embed field value cannot exceed ${Embed.MAX_FIELD_VALUE_LENGTH} characters.`) + } + }) + if (this.fields.length > Embed.MAX_FIELDS_LENGTH) throw new Error('Embed fields cannot exceed 25 field objects.') + } + + if (this.footer?.text?.length !== undefined && this.footer?.text?.length > Embed.MAX_FOOTER_TEXT_LENGTH) { + total += this.footer?.text?.length + throw new Error(`Embed footer text cannot exceed ${Embed.MAX_FOOTER_TEXT_LENGTH}.`) + } + + if (this.author?.name?.length !== undefined && this.author?.name?.length > Embed.MAX_AUTHOR_NAME_LENGTH) { + total += this.author?.name?.length + throw new Error(`Embed author name cannot exceed ${Embed.MAX_AUTHOR_NAME_LENGTH}.`) + } + + if (total > Embed.MAX_EMBED_LENGTH) throw new Error(`Embed characters cannot exceed ${Embed.MAX_EMBED_LENGTH} characters in total.`) + return { title: this.title, type: this.type, From 7e9c86cc2b8590ae232a985fe6e63594eae110d4 Mon Sep 17 00:00:00 2001 From: waterflamev8 Date: Sat, 20 Mar 2021 13:29:53 +0800 Subject: [PATCH 2/2] Make eslint happy --- src/structures/embed.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/structures/embed.ts b/src/structures/embed.ts index 1303f58..5dd32cc 100644 --- a/src/structures/embed.ts +++ b/src/structures/embed.ts @@ -58,24 +58,24 @@ export class Embed { toJSON(): EmbedPayload { let total = 0; if (this.title?.length !== undefined && this.title?.length > Embed.MAX_TITLE_LENGTH) { - total += this.title.length + total += Number(this.title.length) throw new Error(`Embed title cannot exceed ${Embed.MAX_TITLE_LENGTH} characters.`) } if (this.description?.length !== undefined && this.description?.length > Embed.MAX_DESCRIPTION_LENGTH) { - total += this.description.length + total += Number(this.description.length) throw new Error(`Embed description cannot exceed ${Embed.MAX_DESCRIPTION_LENGTH} characters.`) } if (this.fields?.length !== undefined) { this.fields.forEach((field) => { if (field.name.length > Embed.MAX_FIELD_NAME_LENGTH) { - total += field.name.length + total += Number(field.name.length) throw new Error(`Embed field name cannot exceed ${Embed.MAX_FIELD_NAME_LENGTH} characters.`) } if (field.value.length > Embed.MAX_FIELD_VALUE_LENGTH) { - total += field.value.length + total += Number(field.value.length) throw new Error(`Embed field value cannot exceed ${Embed.MAX_FIELD_VALUE_LENGTH} characters.`) } }) @@ -83,12 +83,12 @@ export class Embed { } if (this.footer?.text?.length !== undefined && this.footer?.text?.length > Embed.MAX_FOOTER_TEXT_LENGTH) { - total += this.footer?.text?.length + total += Number(this.footer?.text?.length) throw new Error(`Embed footer text cannot exceed ${Embed.MAX_FOOTER_TEXT_LENGTH}.`) } if (this.author?.name?.length !== undefined && this.author?.name?.length > Embed.MAX_AUTHOR_NAME_LENGTH) { - total += this.author?.name?.length + total += Number(this.author?.name?.length) throw new Error(`Embed author name cannot exceed ${Embed.MAX_AUTHOR_NAME_LENGTH}.`) }