@@ -4,7 +4,7 @@ import path from 'path';
4
4
type AnyObject = { [ k : string ] : any } ;
5
5
6
6
type Template = MessageEmbedOptions ;
7
- type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template }
7
+ type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template } ;
8
8
9
9
type CheckSizeKey = keyof Template | string ;
10
10
type CheckSizeContent = Template [ keyof Template ] ;
@@ -57,19 +57,19 @@ export const limits = {
57
57
export class BetterEmbed extends MessageEmbed {
58
58
public static LENGTH_LIMITS = limits ;
59
59
public static TEMPLATES = templates ;
60
-
60
+
61
61
public constructor ( data ?: MessageEmbed | Template ) {
62
62
super ( data ) ;
63
63
this . checkSize ( ) ;
64
64
}
65
-
65
+
66
66
public static fromTemplate ( template : keyof Templates | Template , values : AnyObject ) : BetterEmbed {
67
67
if ( typeof template === 'string' )
68
68
if ( templates [ template ] ) template = templates [ template ] ;
69
69
else throw new Error ( `Template '${ template } ' not found.` ) ;
70
-
70
+
71
71
template = JSON . parse ( JSON . stringify ( template ) ) ;
72
-
72
+
73
73
function setValues ( object : AnyObject , values : AnyObject ) : Template {
74
74
for ( const [ name , value ] of Object . entries ( object ) ) {
75
75
if ( ! object . hasOwnProperty ( name ) ) continue ;
@@ -78,34 +78,33 @@ export class BetterEmbed extends MessageEmbed {
78
78
object [ name ] = setValues ( value , values ) ;
79
79
continue ;
80
80
}
81
-
81
+
82
82
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
+ } ) ;
86
85
object [ name ] = eval ( `\`${ code } \`` ) ;
87
86
}
88
-
87
+
89
88
return object ;
90
89
}
91
-
90
+
92
91
return new BetterEmbed ( setValues ( template as AnyObject , values ) ) ;
93
92
}
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 ;
96
95
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 } } ;
98
97
public checkSize ( field ?: keyof Template ) {
99
98
if ( ! field ) {
100
- const fields : { [ k in CheckSizeKey ] : { content : CheckSizeContent , limit : number } } = { } ;
101
-
99
+ const fields : { [ k in CheckSizeKey ] : { content : CheckSizeContent ; limit : number } } = { } ;
100
+
102
101
function addField ( name : CheckSizeKey , content : CheckSizeContent , limit : number ) {
103
102
fields [ name ] = {
104
103
content,
105
104
limit,
106
105
} ;
107
106
}
108
-
107
+
109
108
if ( this . title && this . title . length > limits . title ) addField ( 'title' , this . title , limits . title ) ;
110
109
if ( this . author ?. name && this . author . name . length > limits . author . name ) addField ( 'author' , this . author . name , limits . author . name ) ;
111
110
if ( this . description && this . description . length > limits . description ) addField ( 'description' , this . description , limits . description ) ;
@@ -114,10 +113,10 @@ export class BetterEmbed extends MessageEmbed {
114
113
if ( field . name ?. length > limits . fields . name ) addField ( `field[${ index } ]` , field . name , limits . fields . name ) ;
115
114
if ( field . value ?. length > limits . fields . value ) addField ( `field[${ index } ]` , field . value , limits . fields . value ) ;
116
115
} ) ;
117
-
116
+
118
117
return fields ;
119
118
}
120
-
119
+
121
120
switch ( field ) {
122
121
case 'fields' :
123
122
if ( this . fields ?. length ) {
@@ -153,19 +152,19 @@ export class BetterEmbed extends MessageEmbed {
153
152
return true ;
154
153
}
155
154
}
156
-
155
+
157
156
public setImageFromFile ( link : string ) {
158
157
const attachment = new MessageAttachment ( link , path . basename ( link ) ) ;
159
158
this . attachFiles ( [ attachment ] ) ;
160
159
this . setImage ( `attachment://${ attachment . name } ` ) ;
161
160
}
162
-
161
+
163
162
public setThumbnailFromFile ( link : string ) {
164
163
const attachment = new MessageAttachment ( link , path . basename ( link ) ) ;
165
164
this . attachFiles ( [ attachment ] ) ;
166
165
this . setThumbnail ( `attachment://${ attachment . name } ` ) ;
167
166
}
168
-
167
+
169
168
public throwIfTooLong ( field : keyof Template ) : void ;
170
169
public throwIfTooLong ( field ?: keyof Template ) {
171
170
if ( field ) {
@@ -177,7 +176,7 @@ export class BetterEmbed extends MessageEmbed {
177
176
case 'description' :
178
177
if ( field === 'author' ? ! this . author ?. name ?. length : ! this [ field ] ?. length ) return ;
179
178
const name = field === 'author' ? 'author.name' : field ;
180
-
179
+
181
180
const limit = field === 'author' ? limits . author . name : limits [ field ] ;
182
181
const length = field === 'author' ? this . author ! . name ! . length : this [ field ] ! . length ;
183
182
throw new RangeError ( `'embed.${ name } ' is too long: ${ length } (max: ${ limit } ).` ) ;
@@ -191,7 +190,7 @@ export class BetterEmbed extends MessageEmbed {
191
190
}
192
191
}
193
192
}
194
-
193
+
195
194
if ( this . title && this . title . length > limits . title ) throw new RangeError ( `'embed.title' is too long: ${ this . title . length } (max: ${ limits . title } ).` ) ;
196
195
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 } ).` ) ;
197
196
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 {
202
201
throw new RangeError ( `'embed.fields[${ this . fields . indexOf ( field ) } ].value' is too long: ${ field . value . length } (max: ${ limits . fields . value } ).` ) ;
203
202
} ) ;
204
203
}
205
-
204
+
206
205
public cutIfTooLong ( ) {
207
206
function cutWithLength ( text : string , maxLength : number ) {
208
207
return text . length > maxLength ? `${ text . substring ( 0 , maxLength - 3 ) } ...` : text ;
209
208
}
210
-
209
+
211
210
if ( this . author ?. name ) this . author . name = cutWithLength ( this . author . name , limits . author . name ) ;
212
211
if ( this . description ) this . description = cutWithLength ( this . description , limits . description ) ;
213
212
if ( this . title ) this . title = cutWithLength ( this . title , limits . title ) ;
0 commit comments