@lunibee/builders 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/embed.ts +16 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunibee/builders",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -8,6 +8,6 @@
8
8
  ".": "./src/index.ts"
9
9
  },
10
10
  "dependencies": {
11
- "@lunibee/types": "^0.3.0"
11
+ "@lunibee/types": "^0.3.1"
12
12
  }
13
13
  }
package/src/embed.ts CHANGED
@@ -198,8 +198,23 @@ export class CreateEmbed {
198
198
  public get data(): APIEmbed {
199
199
  return this.toJSON();
200
200
  }
201
+ /** Returns a deep-cloned Discord API payload. @returns Strict embed payload. @throws {RangeError} If the text in the embed adds up to more than Discord's 6000-character limit. */
201
202
  public toJSON(): APIEmbed {
202
- return structuredClone(this.#data);
203
+ const data = this.#data;
204
+ const total =
205
+ (data.title?.length ?? 0) +
206
+ (data.description?.length ?? 0) +
207
+ (data.footer?.text.length ?? 0) +
208
+ (data.author?.name.length ?? 0) +
209
+ (data.fields ?? []).reduce(
210
+ (sum, field) => sum + field.name.length + field.value.length,
211
+ 0,
212
+ );
213
+ if (total > 6000)
214
+ throw new RangeError(
215
+ `An embed cannot contain more than 6000 characters in total (this one has ${total}).`,
216
+ );
217
+ return structuredClone(data);
203
218
  }
204
219
  }
205
220
  /** Validates a bounded Discord string field. @param value Field value. @param max Maximum length. @param field Human-readable field name. @returns Validated value. @throws {RangeError} If length is invalid. */