Skip to content

Commit a7e396d

Browse files
committed
feat: Add throwIfTooLong method.
1 parent 0f27a2e commit a7e396d

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/BetterEmbed.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ export class BetterEmbed extends MessageEmbed {
7878
continue;
7979
}
8080

81-
const code = value.replace(/\$\{([^}]+)\}/gu, (_: any, value: string) => (values.hasOwnProperty(value.split('.')[0])
82-
? `\${values.${value}}`
83-
: value));
81+
const code = value.replace(/\$\{([^}]+)\}/gu, (_: any, value: string) => {
82+
return values.hasOwnProperty(value.split('.')[0]) ? `\${values.${value}}` : value;
83+
},
84+
);
8485
object[name] = eval(`\`${code}\``);
8586
}
8687

@@ -152,7 +153,32 @@ export class BetterEmbed extends MessageEmbed {
152153
}
153154
}
154155

155-
public throwIfTooLong() {
156+
public throwIfTooLong(field: keyof Template): void;
157+
public throwIfTooLong(field?: keyof Template) {
158+
if (field) {
159+
const tooLong = this.checkSize(field);
160+
if (!tooLong) return;
161+
switch (field) {
162+
case 'title':
163+
case 'author':
164+
case 'description':
165+
if (field === 'author' ? !this.author?.name?.length : !this[field]?.length) return;
166+
const name = field === 'author' ? 'author.name' : field;
167+
168+
const limit = field === 'author' ? limits.author.name : limits[field];
169+
const length = field === 'author' ? this.author!.name!.length : this[field]!.length;
170+
throw new RangeError(`'embed.${name}' is too long: ${length} (max: ${limit}).`);
171+
case 'fields':
172+
const tooLongFields = this.checkSize(field);
173+
if (typeof tooLongFields === 'boolean') throw new RangeError(`Too much fields (${limits.fields.size}).`);
174+
else {
175+
const name = 'name' in tooLongFields ? 'value' : 'name';
176+
// TODO : Find a fix for typings.
177+
throw new RangeError(`'embed.fields[${tooLongFields.index}].${name}' is too long: ${(tooLongFields as any)[name]!.length}`);
178+
}
179+
}
180+
}
181+
156182
if (this.title && this.title.length > limits.title) throw new RangeError(`'embed.title' is too long: ${this.title.length} (max: ${limits.title}).`);
157183
if (this.author?.name && this.author.name.length > limits.author.name) throw new RangeError(`'embed.author.name' is too long: ${this.author.name.length} (max: ${limits.author.name}).`);
158184
if (this.description && this.description.length > limits.description) throw new RangeError(`'embed.description' is too long: ${this.description.length} (max: ${limits.description}).`);

0 commit comments

Comments
 (0)