@@ -4,7 +4,7 @@ type AnyObject = {[k: string]: any};
44
55type Template = MessageEmbedOptions ;
66
7- type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template }
7+ type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template }
88
99export const templates : Templates = {
1010 basic : {
@@ -52,18 +52,21 @@ export const limits = {
5252} ;
5353
5454export class BetterEmbed extends MessageEmbed {
55+ public static LENGTH_LIMITS = limits ;
56+ public static TEMPLATES = templates ;
57+
5558 public constructor ( data ?: MessageEmbed | Template ) {
5659 super ( data ) ;
5760 this . checkSize ( ) ;
5861 }
59-
62+
6063 public static fromTemplate ( template : keyof Templates | Template , values : AnyObject ) : BetterEmbed {
6164 if ( typeof template === 'string' )
6265 if ( templates [ template ] ) template = templates [ template ] ;
6366 else throw new Error ( `Template '${ template } ' not found.` ) ;
64-
67+
6568 template = JSON . parse ( JSON . stringify ( template ) ) ;
66-
69+
6770 function setValues ( object : AnyObject , values : AnyObject ) : Template {
6871 for ( const [ name , value ] of Object . entries ( object ) ) {
6972 if ( ! object . hasOwnProperty ( name ) ) continue ;
@@ -72,22 +75,84 @@ export class BetterEmbed extends MessageEmbed {
7275 object [ name ] = setValues ( value , values ) ;
7376 continue ;
7477 }
75-
76- const code = value . replace ( / \$ \{ ( [ ^ } ] + ) \} / gu, ( _ : any , value : string ) => ( values . hasOwnProperty ( value . split ( '.' ) [ 0 ] ) ? `\${values.${ value } }` : value ) ) ;
78+
79+ const code = value . replace ( / \$ \{ ( [ ^ } ] + ) \} / gu, ( _ : any , value : string ) => ( values . hasOwnProperty ( value . split ( '.' ) [ 0 ] )
80+ ? `\${values.${ value } }`
81+ : value ) ) ;
7782 object [ name ] = eval ( `\`${ code } \`` ) ;
7883 }
79-
84+
8085 return object ;
8186 }
82-
87+
8388 return new BetterEmbed ( setValues ( template as AnyObject , values ) ) ;
8489 }
8590
86- public static LENGTH_LIMITS = limits ;
91+ public checkSize ( field : 'fields' ) : { index : number , limit : number } & ( { name : boolean } | { value : boolean } ) | boolean
92+ public checkSize ( field : keyof Template ) : boolean ;
93+ public checkSize ( ) : { [ k in keyof Template | string ] : { content : string | Template [ keyof Template ] , limit : number } }
94+ public checkSize ( field ?: keyof Template ) {
95+ if ( ! field ) {
96+ type key = keyof Template | string ;
97+ type content = string | Template [ keyof Template ]
98+ const fields : { [ k in key ] : { content : content , limit : number } } = { } ;
99+
100+ function addField ( name : key , content : content , limit : number ) {
101+ fields [ name ] = {
102+ content,
103+ limit,
104+ } ;
105+ }
106+
107+ if ( this . title && this . title . length > limits . title ) addField ( 'title' , this . title , limits . title ) ;
108+ if ( this . author ?. name && this . author . name . length > limits . author . name ) addField ( 'author' , this . author . name , limits . author . name ) ;
109+ if ( this . description && this . description . length > limits . description ) addField ( 'description' , this . description , limits . description ) ;
110+ if ( this . fields ?. length > limits . fields . size ) addField ( 'fields' , this . fields , limits . fields . size ) ;
111+ this . fields . forEach ( ( field , index ) => {
112+ if ( field . name ?. length > limits . fields . name ) addField ( `field[${ index } ]` , field . name , limits . fields . name ) ;
113+ if ( field . value ?. length > limits . fields . value ) addField ( `field[${ index } ]` , field . value , limits . fields . value ) ;
114+ } ) ;
115+
116+ return fields ;
117+ }
118+
119+ switch ( field ) {
120+ case 'fields' :
121+ if ( this . fields ?. length ) {
122+ return this . fields . length > limits . fields . size ;
123+ } else {
124+ for ( const field of this . fields ) {
125+ const index = this . fields . indexOf ( field ) ;
126+ if ( field . name . length > limits . fields . name ) {
127+ return {
128+ index,
129+ name : true ,
130+ limit : limits . fields . name
131+ }
132+ } else if ( field . value . length > limits . fields . value ) {
133+ return {
134+ index,
135+ value : true ,
136+ limit : limits . fields . value
137+ }
138+ }
139+ }
140+ return false ;
141+ }
142+ case 'footer' :
143+ return this . footer ?. text ? this . footer . text . length > limits . footer . text : true ;
144+ case 'title' :
145+ return this . title ? this . title ?. length > limits . title : true ;
146+ case 'author' :
147+ return this . author ?. name ? this . author . name . length > limits . author . name : true ;
148+ case 'description' :
149+ return this . description ? this . description . length > limits . description : true ;
150+ default :
151+ return true ;
152+ }
153+ }
87154
88- public static TEMPLATES = templates ;
89-
90- public checkSize ( ) {
155+ public throwIfTooLong ( ) {
91156 if ( this . title && this . title . length > limits . title ) throw new RangeError ( `'embed.title' is too long: ${ this . title . length } (max: ${ limits . title } ).` ) ;
92157 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 } ).` ) ;
93158 if ( this . description && this . description . length > limits . description ) throw new RangeError ( `'embed.description' is too long: ${ this . description . length } (max: ${ limits . description } ).` ) ;
@@ -98,12 +163,12 @@ export class BetterEmbed extends MessageEmbed {
98163 throw new RangeError ( `'embed.fields[${ this . fields . indexOf ( field ) } ].value' is too long: ${ field . value . length } (max: ${ limits . fields . value } ).` ) ;
99164 } ) ;
100165 }
101-
166+
102167 public cutIfTooLong ( ) {
103168 function cutWithLength ( text : string , maxLength : number ) {
104169 return text . length > maxLength ? `${ text . substring ( 0 , maxLength - 3 ) } ...` : text ;
105170 }
106-
171+
107172 if ( this . author ?. name ) this . author . name = cutWithLength ( this . author . name , limits . author . name ) ;
108173 if ( this . description ) this . description = cutWithLength ( this . description , limits . description ) ;
109174 if ( this . title ) this . title = cutWithLength ( this . title , limits . title ) ;
0 commit comments