1- import { MessageAttachment , MessageEmbed } from 'discord.js' ;
1+ import type { APIEmbed } from 'discord-api-types/v10' ;
2+ import { AttachmentBuilder , Embed , EmbedBuilder } from 'discord.js' ;
23import { AnyObject , CheckSizeContent , CheckSizeKey , Template , TemplatesValues } from './types' ;
34
45export const templates : TemplatesValues = {
@@ -12,7 +13,7 @@ export const templates: TemplatesValues = {
1213 color : {
1314 color : '#4b5afd' ,
1415 } ,
15- //@ts -ignore
16+ // @ts -ignore
1617 get complete ( ) {
1718 return {
1819 ...this . basic ,
@@ -51,41 +52,86 @@ export type Templates = typeof templates;
5152type ValuesFromTemplateKey < T extends string , P = Templates [ T ] > = P extends Template < infer V > ? V : { } ;
5253type Values < T extends string | Template > = T extends Template < infer V > ? V : T extends string ? ValuesFromTemplateKey < T > : AnyObject | undefined ;
5354
54- export class BetterEmbed extends MessageEmbed {
55+ // @ts -ignore
56+ export class BetterEmbed extends EmbedBuilder {
5557 public static LENGTH_LIMITS = limits ;
5658 public static TEMPLATES = templates ;
5759
58- public constructor ( data ?: MessageEmbed | Template ) {
60+ public constructor ( data ?: APIEmbed | Template ) {
5961 super ( data ) ;
6062 this . checkSize ( ) ;
6163 }
6264
65+ get author ( ) {
66+ return this . data . author as Embed [ 'author' ] ;
67+ }
68+
69+ get color ( ) {
70+ return this . data . color ;
71+ }
72+
73+ get description ( ) {
74+ return this . data . description ;
75+ }
76+
77+ get fields ( ) {
78+ return this . data . fields as Embed [ 'fields' ] ;
79+ }
80+
81+ get footer ( ) {
82+ return this . data . footer as Embed [ 'footer' ] ;
83+ }
84+
85+ get image ( ) {
86+ return this . data . image as Embed [ 'image' ] ;
87+ }
88+
89+ get thumbnail ( ) {
90+ return this . data . thumbnail as Embed [ 'thumbnail' ] ;
91+ }
92+
93+ get title ( ) {
94+ return this . data . title ;
95+ }
96+
97+ get timestamp ( ) {
98+ return this . data . timestamp ;
99+ }
100+
101+ get url ( ) {
102+ return this . data . url ;
103+ }
104+
63105 public static isTemplate ( key : string ) : key is keyof Templates & string {
64106 return templates [ key ] !== undefined ;
65107 }
66108
67- public static fromTemplate < T extends ( keyof Templates & string ) | Template , V extends AnyObject | undefined = Values < T > > (
68- template : T ,
69- values ?: V
70- ) : BetterEmbed {
71- if ( typeof template === 'string' )
72- if ( templates [ template ] ) template = templates [ template ] as T ;
73- else throw new Error ( `Template '${ template } ' not found.` ) ;
109+ public static fromTemplate < T extends ( keyof Templates & string ) | Template , V extends AnyObject | undefined = Values < T > > ( template : T , values ?: V ) : BetterEmbed {
110+ if ( typeof template === 'string' ) {
111+ if ( templates [ template ] ) {
112+ template = templates [ template ] as T ;
113+ } else {
114+ throw new Error ( `Template '${ template } ' not found.` ) ;
115+ }
116+ }
74117
75118 template = JSON . parse ( JSON . stringify ( template ) ) ;
76119
77120 function setValues ( object : AnyObject , values : AnyObject = { } ) : Template {
78121 for ( const [ name , value ] of Object . entries ( object ) ) {
79122 if ( ! object . hasOwnProperty ( name ) ) continue ;
123+
80124 if ( Array . isArray ( value ) ) object [ name ] = value . map ( v => setValues ( v , values ) ) ;
125+ if ( typeof value === 'number' ) {
126+ object [ name ] = value ;
127+ continue ;
128+ }
81129 if ( typeof value === 'object' ) {
82130 object [ name ] = setValues ( value , values ) ;
83131 continue ;
84132 }
85133
86- const code = value . replace ( / \$ \{ ( [ ^ } ] + ) \} / gu, ( _ : any , value : string ) => {
87- return values . hasOwnProperty ( value . split ( '.' ) [ 0 ] ) ? `\${values.${ value } }` : value ;
88- } ) ;
134+ const code = value . replace ( / \$ \{ ( [ ^ } ] + ) \} / gu, ( _ : any , value : string ) => ( values . hasOwnProperty ( value . split ( '.' ) [ 0 ] ) ? `\${values.${ value } }` : value ) ) ;
89135 object [ name ] = eval ( `\`${ code } \`` ) ;
90136 }
91137
@@ -95,12 +141,34 @@ export class BetterEmbed extends MessageEmbed {
95141 return new BetterEmbed ( setValues ( template as AnyObject , values ) ) ;
96142 }
97143
98- public checkSize ( field : 'fields' ) : ( { index : number ; limit : number } & ( { name : boolean } | { value : boolean } ) ) | boolean ;
144+ public checkSize ( field : 'fields' ) :
145+ | ( {
146+ index : number ;
147+ limit : number ;
148+ } & (
149+ | {
150+ name : boolean ;
151+ }
152+ | {
153+ value : boolean ;
154+ }
155+ ) )
156+ | boolean ;
99157 public checkSize ( field : keyof Template ) : boolean ;
100- public checkSize ( ) : { [ k in CheckSizeKey ] : { content : CheckSizeContent ; limit : number } } ;
158+ public checkSize ( ) : {
159+ [ k in CheckSizeKey ] : {
160+ content : CheckSizeContent ;
161+ limit : number ;
162+ } ;
163+ } ;
101164 public checkSize ( field ?: keyof Template ) {
102165 if ( ! field ) {
103- const fields : { [ k in CheckSizeKey ] : { content : CheckSizeContent ; limit : number } } = { } ;
166+ const fields : {
167+ [ k in CheckSizeKey ] : {
168+ content : CheckSizeContent ;
169+ limit : number ;
170+ } ;
171+ } = { } ;
104172
105173 function addField ( name : CheckSizeKey , content : CheckSizeContent , limit : number ) {
106174 fields [ name ] = {
@@ -157,11 +225,11 @@ export class BetterEmbed extends MessageEmbed {
157225 }
158226 }
159227
160- public setImageFromFile ( attachment : MessageAttachment ) {
228+ public setImageFromFile ( attachment : AttachmentBuilder ) {
161229 this . setImage ( `attachment://${ attachment . name } ` ) ;
162230 }
163231
164- public setThumbnailFromFile ( attachment : MessageAttachment ) {
232+ public setThumbnailFromFile ( attachment : AttachmentBuilder ) {
165233 this . setThumbnail ( `attachment://${ attachment . name } ` ) ;
166234 }
167235
@@ -182,29 +250,32 @@ export class BetterEmbed extends MessageEmbed {
182250 throw new RangeError ( `'embed.${ name } ' is too long: ${ length } (max: ${ limit } ).` ) ;
183251 case 'fields' :
184252 const tooLongFields = this . checkSize ( field ) ;
185- if ( typeof tooLongFields === 'boolean' ) throw new RangeError ( `Too much fields (${ limits . fields . size } ).` ) ;
186- else {
253+ if ( typeof tooLongFields === 'boolean' ) {
254+ throw new RangeError ( `Too much fields (${ limits . fields . size } ).` ) ;
255+ } else {
187256 const name = 'name' in tooLongFields ? 'value' : 'name' ;
188257 throw new RangeError (
189- `'embed.fields[${ tooLongFields . index } ].${ name } ' is too long: ${ this . fields [ tooLongFields . index ] [ name ] . length } (max: ${
190- tooLongFields . limit
191- } )`
258+ `'embed.fields[${ tooLongFields . index } ].${ name } ' is too long: ${ this . fields [ tooLongFields . index ] [ name ] . length } (max: ${ tooLongFields . limit } )` ,
192259 ) ;
193260 }
194261 }
195262 }
196263
197264 if ( this . title && this . title . length > limits . title ) throw new RangeError ( `'embed.title' is too long: ${ this . title . length } (max: ${ limits . title } ).` ) ;
198- if ( this . author ?. name && this . author . name . length > limits . author . name )
265+ if ( this . author ?. name && this . author . name . length > limits . author . name ) {
199266 throw new RangeError ( `'embed.author.name' is too long: ${ this . author . name . length } (max: ${ limits . author . name } ).` ) ;
200- if ( this . description && this . description . length > limits . description )
267+ }
268+ if ( this . description && this . description . length > limits . description ) {
201269 throw new RangeError ( `'embed.description' is too long: ${ this . description . length } (max: ${ limits . description } ).` ) ;
270+ }
202271 if ( this . fields ?. length > limits . fields . size ) throw new RangeError ( `Too much fields (${ limits . fields . size } ).` ) ;
203272 this . fields . forEach ( field => {
204- if ( field . name ?. length > limits . fields . name )
273+ if ( field . name ?. length > limits . fields . name ) {
205274 throw new RangeError ( `'embed.fields[${ this . fields . indexOf ( field ) } ].name' is too long: ${ field . name . length } (max: ${ limits . fields . name } ).` ) ;
206- if ( field . value ?. length > limits . fields . value )
275+ }
276+ if ( field . value ?. length > limits . fields . value ) {
207277 throw new RangeError ( `'embed.fields[${ this . fields . indexOf ( field ) } ].value' is too long: ${ field . value . length } (max: ${ limits . fields . value } ).` ) ;
278+ }
208279 } ) ;
209280 }
210281
@@ -214,10 +285,10 @@ export class BetterEmbed extends MessageEmbed {
214285 }
215286
216287 if ( this . author ?. name ) this . author . name = cutWithLength ( this . author . name , limits . author . name ) ;
217- if ( this . description ) this . description = cutWithLength ( this . description , limits . description ) ;
218- if ( this . title ) this . title = cutWithLength ( this . title , limits . title ) ;
288+ if ( this . description ) this . setDescription ( cutWithLength ( this . description , limits . description ) ) ;
289+ if ( this . title ) this . setTitle ( cutWithLength ( this . title , limits . title ) ) ;
219290 if ( this . fields ) {
220- if ( this . fields . length > limits . fields . size ) this . fields = this . fields . slice ( 0 , limits . fields . size ) ?? [ ] ;
291+ if ( this . fields . length > limits . fields . size ) this . setFields ( this . fields . slice ( 0 , limits . fields . size ) ?? [ ] ) ;
221292 this . fields . forEach ( field => {
222293 field . name = cutWithLength ( field . name ?? '' , limits . fields . name ) ;
223294 field . value = cutWithLength ( field . value ?? '' , limits . fields . value ) ;
0 commit comments