@@ -4,7 +4,7 @@ import path from 'path';
44type AnyObject = { [ k : string ] : any } ;
55
66type Template = MessageEmbedOptions ;
7- type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template }
7+ type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template } ;
88
99type CheckSizeKey = keyof Template | string ;
1010type CheckSizeContent = Template [ keyof Template ] ;
@@ -57,19 +57,19 @@ export const limits = {
5757export class BetterEmbed extends MessageEmbed {
5858 public static LENGTH_LIMITS = limits ;
5959 public static TEMPLATES = templates ;
60-
60+
6161 public constructor ( data ?: MessageEmbed | Template ) {
6262 super ( data ) ;
6363 this . checkSize ( ) ;
6464 }
65-
65+
6666 public static fromTemplate ( template : keyof Templates | Template , values : AnyObject ) : BetterEmbed {
6767 if ( typeof template === 'string' )
6868 if ( templates [ template ] ) template = templates [ template ] ;
6969 else throw new Error ( `Template '${ template } ' not found.` ) ;
70-
70+
7171 template = JSON . parse ( JSON . stringify ( template ) ) ;
72-
72+
7373 function setValues ( object : AnyObject , values : AnyObject ) : Template {
7474 for ( const [ name , value ] of Object . entries ( object ) ) {
7575 if ( ! object . hasOwnProperty ( name ) ) continue ;
@@ -78,34 +78,33 @@ export class BetterEmbed extends MessageEmbed {
7878 object [ name ] = setValues ( value , values ) ;
7979 continue ;
8080 }
81-
81+
8282 const code = value . replace ( / \$ \{ ( [ ^ } ] + ) \} / gu, ( _ : any , value : string ) => {
83- return values . hasOwnProperty ( value . split ( '.' ) [ 0 ] ) ? `\${values.${ value } }` : value ;
84- } ,
85- ) ;
83+ return values . hasOwnProperty ( value . split ( '.' ) [ 0 ] ) ? `\${values.${ value } }` : value ;
84+ } ) ;
8685 object [ name ] = eval ( `\`${ code } \`` ) ;
8786 }
88-
87+
8988 return object ;
9089 }
91-
90+
9291 return new BetterEmbed ( setValues ( template as AnyObject , values ) ) ;
9392 }
94-
95- public checkSize ( field : 'fields' ) : { index : number , limit : number } & ( { name : boolean } | { value : boolean } ) | boolean
93+
94+ public checkSize ( field : 'fields' ) : ( { index : number ; limit : number } & ( { name : boolean } | { value : boolean } ) ) | boolean ;
9695 public checkSize ( field : keyof Template ) : boolean ;
97- public checkSize ( ) : { [ k in CheckSizeKey ] : { content : CheckSizeContent , limit : number } }
96+ public checkSize ( ) : { [ k in CheckSizeKey ] : { content : CheckSizeContent ; limit : number } } ;
9897 public checkSize ( field ?: keyof Template ) {
9998 if ( ! field ) {
100- const fields : { [ k in CheckSizeKey ] : { content : CheckSizeContent , limit : number } } = { } ;
101-
99+ const fields : { [ k in CheckSizeKey ] : { content : CheckSizeContent ; limit : number } } = { } ;
100+
102101 function addField ( name : CheckSizeKey , content : CheckSizeContent , limit : number ) {
103102 fields [ name ] = {
104103 content,
105104 limit,
106105 } ;
107106 }
108-
107+
109108 if ( this . title && this . title . length > limits . title ) addField ( 'title' , this . title , limits . title ) ;
110109 if ( this . author ?. name && this . author . name . length > limits . author . name ) addField ( 'author' , this . author . name , limits . author . name ) ;
111110 if ( this . description && this . description . length > limits . description ) addField ( 'description' , this . description , limits . description ) ;
@@ -114,10 +113,10 @@ export class BetterEmbed extends MessageEmbed {
114113 if ( field . name ?. length > limits . fields . name ) addField ( `field[${ index } ]` , field . name , limits . fields . name ) ;
115114 if ( field . value ?. length > limits . fields . value ) addField ( `field[${ index } ]` , field . value , limits . fields . value ) ;
116115 } ) ;
117-
116+
118117 return fields ;
119118 }
120-
119+
121120 switch ( field ) {
122121 case 'fields' :
123122 if ( this . fields ?. length ) {
@@ -153,19 +152,19 @@ export class BetterEmbed extends MessageEmbed {
153152 return true ;
154153 }
155154 }
156-
155+
157156 public setImageFromFile ( link : string ) {
158157 const attachment = new MessageAttachment ( link , path . basename ( link ) ) ;
159158 this . attachFiles ( [ attachment ] ) ;
160159 this . setImage ( `attachment://${ attachment . name } ` ) ;
161160 }
162-
161+
163162 public setThumbnailFromFile ( link : string ) {
164163 const attachment = new MessageAttachment ( link , path . basename ( link ) ) ;
165164 this . attachFiles ( [ attachment ] ) ;
166165 this . setThumbnail ( `attachment://${ attachment . name } ` ) ;
167166 }
168-
167+
169168 public throwIfTooLong ( field : keyof Template ) : void ;
170169 public throwIfTooLong ( field ?: keyof Template ) {
171170 if ( field ) {
@@ -177,7 +176,7 @@ export class BetterEmbed extends MessageEmbed {
177176 case 'description' :
178177 if ( field === 'author' ? ! this . author ?. name ?. length : ! this [ field ] ?. length ) return ;
179178 const name = field === 'author' ? 'author.name' : field ;
180-
179+
181180 const limit = field === 'author' ? limits . author . name : limits [ field ] ;
182181 const length = field === 'author' ? this . author ! . name ! . length : this [ field ] ! . length ;
183182 throw new RangeError ( `'embed.${ name } ' is too long: ${ length } (max: ${ limit } ).` ) ;
@@ -191,7 +190,7 @@ export class BetterEmbed extends MessageEmbed {
191190 }
192191 }
193192 }
194-
193+
195194 if ( this . title && this . title . length > limits . title ) throw new RangeError ( `'embed.title' is too long: ${ this . title . length } (max: ${ limits . title } ).` ) ;
196195 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 } ).` ) ;
197196 if ( this . description && this . description . length > limits . description ) throw new RangeError ( `'embed.description' is too long: ${ this . description . length } (max: ${ limits . description } ).` ) ;
@@ -202,12 +201,12 @@ export class BetterEmbed extends MessageEmbed {
202201 throw new RangeError ( `'embed.fields[${ this . fields . indexOf ( field ) } ].value' is too long: ${ field . value . length } (max: ${ limits . fields . value } ).` ) ;
203202 } ) ;
204203 }
205-
204+
206205 public cutIfTooLong ( ) {
207206 function cutWithLength ( text : string , maxLength : number ) {
208207 return text . length > maxLength ? `${ text . substring ( 0 , maxLength - 3 ) } ...` : text ;
209208 }
210-
209+
211210 if ( this . author ?. name ) this . author . name = cutWithLength ( this . author . name , limits . author . name ) ;
212211 if ( this . description ) this . description = cutWithLength ( this . description , limits . description ) ;
213212 if ( this . title ) this . title = cutWithLength ( this . title , limits . title ) ;
0 commit comments