Skip to content

Commit 21d88fd

Browse files
committed
Added cutIfTooLong method.
1 parent c0e4a88 commit 21d88fd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/BetterEmbed.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,21 @@ export default class BetterEmbed extends MessageEmbed {
6262
if (field.value?.length > BetterEmbed.limits.fields.value) throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].value is too long (${BetterEmbed.limits.fields.value}).`);
6363
});
6464
}
65+
66+
cutIfTooLong() {
67+
function cutWithLength(text: string, maxLength: number) {
68+
return text.length > maxLength ? `${text.substring(0, maxLength - 3)}...` : text;
69+
}
70+
71+
if (this.author?.name) this.author.name = cutWithLength(this.author.name, BetterEmbed.limits.author.name);
72+
if(this.description) this.description = cutWithLength(this.description, BetterEmbed.limits.description);
73+
if(this.title) this.title = cutWithLength(this.title, BetterEmbed.limits.title);
74+
if (this.fields) {
75+
if (this.fields.length > BetterEmbed.limits.fields.size) this.fields = this.fields.slice(0, BetterEmbed.limits.fields.size) ?? [];
76+
this.fields.forEach(field => {
77+
field.name = cutWithLength(field.name ?? '', BetterEmbed.limits.fields.name);
78+
field.value = cutWithLength(field.value ?? '', BetterEmbed.limits.fields.value);
79+
});
80+
}
81+
}
6582
}

0 commit comments

Comments
 (0)