1- import { MessageAttachment , MessageEmbed , MessageEmbedOptions } from 'discord.js' ;
2- import path from 'path' ;
3-
4- type AnyObject = { [ k : string ] : any } ;
5-
6- type Template = MessageEmbedOptions ;
7- type Templates = { [ k in string | 'basic' | 'color' | 'complete' | 'image' ] : Template } ;
8-
9- type CheckSizeKey = keyof Template | string ;
10- type CheckSizeContent = Template [ keyof Template ] ;
11-
12- export const templates : Templates = {
13- basic : {
14- footer : {
15- text : '${client.user.username}' ,
16- iconURL : '${client.user.displayAvatarURL()}' ,
17- } ,
18- timestamp : new Date ( ) ,
19- } ,
20- color : {
21- color : '#4b5afd' ,
22- } ,
23- get complete ( ) {
24- return {
25- ...this . basic ,
26- ...this . color ,
27- title : '${title}' ,
28- description : '${description}' ,
29- } ;
30- } ,
31- get image ( ) {
32- return {
33- ...this . complete ,
34- image : {
35- url : '${image}' ,
36- } ,
37- } ;
38- } ,
1+ import { MessageAttachment , MessageEmbed } from 'discord.js' ;
2+ import { AnyObject , CheckSizeContent , CheckSizeKey , Template , TemplatesValues } from './types'
3+
4+ export const templates : TemplatesValues = {
5+ basic : {
6+ footer : {
7+ text : '${client.user.username}' ,
8+ iconURL : '${client.user.displayAvatarURL()}' ,
9+ } ,
10+ timestamp : new Date ( )
11+ } ,
12+ color : {
13+ color : '#4b5afd'
14+ } ,
15+ //@ts -ignore
16+ get complete ( ) {
17+ return {
18+ ...this . basic ,
19+ ...this . color ,
20+ description : '${description}' ,
21+ title : '${title}'
22+ }
23+ } ,
24+ get image ( ) {
25+ return {
26+ ...this . complete ,
27+ image : {
28+ url : '${url}'
29+ }
30+ }
31+ }
3932} ;
4033
4134export const limits = {
@@ -54,6 +47,10 @@ export const limits = {
5447 } ,
5548} ;
5649
50+ export type Templates = typeof templates
51+ type ValuesFromTemplateKey < T extends ( string ) , P = Templates [ T ] > = P extends Template ? P [ 'values' ] : { } ;
52+ type Values < T extends ( string ) | Template > = T extends Template ? T [ 'values' ] : T extends string ? ValuesFromTemplateKey < T > : AnyObject | undefined ;
53+
5754export class BetterEmbed extends MessageEmbed {
5855 public static LENGTH_LIMITS = limits ;
5956 public static TEMPLATES = templates ;
@@ -63,14 +60,18 @@ export class BetterEmbed extends MessageEmbed {
6360 this . checkSize ( ) ;
6461 }
6562
66- public static fromTemplate ( template : keyof Templates | Template , values : AnyObject ) : BetterEmbed {
63+ public static isTemplate ( key : string ) : key is keyof Templates & string {
64+ return templates [ key ] !== undefined ;
65+ }
66+
67+ public static fromTemplate < T extends ( keyof Templates & string ) | Template , V extends AnyObject | undefined = Values < T > > ( template : T , values ?: V ) : BetterEmbed {
6768 if ( typeof template === 'string' )
68- if ( templates [ template ] ) template = templates [ template ] ;
69+ if ( templates [ template ] ) template = templates [ template ] as T ;
6970 else throw new Error ( `Template '${ template } ' not found.` ) ;
7071
7172 template = JSON . parse ( JSON . stringify ( template ) ) ;
7273
73- function setValues ( object : AnyObject , values : AnyObject ) : Template {
74+ function setValues ( object : AnyObject , values : AnyObject = { } ) : Template {
7475 for ( const [ name , value ] of Object . entries ( object ) ) {
7576 if ( ! object . hasOwnProperty ( name ) ) continue ;
7677 if ( Array . isArray ( value ) ) object [ name ] = value . map ( v => setValues ( v , values ) ) ;
@@ -153,15 +154,11 @@ export class BetterEmbed extends MessageEmbed {
153154 }
154155 }
155156
156- public setImageFromFile ( link : string ) {
157- const attachment = new MessageAttachment ( link , path . basename ( link ) ) ;
158- this . attachFiles ( [ attachment ] ) ;
157+ public setImageFromFile ( attachment : MessageAttachment ) {
159158 this . setImage ( `attachment://${ attachment . name } ` ) ;
160159 }
161160
162- public setThumbnailFromFile ( link : string ) {
163- const attachment = new MessageAttachment ( link , path . basename ( link ) ) ;
164- this . attachFiles ( [ attachment ] ) ;
161+ public setThumbnailFromFile ( attachment : MessageAttachment ) {
165162 this . setThumbnail ( `attachment://${ attachment . name } ` ) ;
166163 }
167164
@@ -185,8 +182,7 @@ export class BetterEmbed extends MessageEmbed {
185182 if ( typeof tooLongFields === 'boolean' ) throw new RangeError ( `Too much fields (${ limits . fields . size } ).` ) ;
186183 else {
187184 const name = 'name' in tooLongFields ? 'value' : 'name' ;
188- // TODO : Find a fix for typings.
189- throw new RangeError ( `'embed.fields[${ tooLongFields . index } ].${ name } ' is too long: ${ ( tooLongFields as any ) [ name ] ! . length } ` ) ;
185+ throw new RangeError ( `'embed.fields[${ tooLongFields . index } ].${ name } ' is too long: ${ this . fields [ tooLongFields . index ] [ name ] . length } (max: ${ tooLongFields . limit } )` ) ;
190186 }
191187 }
192188 }
0 commit comments