Skip to content

Commit 5499af8

Browse files
committed
refactor: Simplify types.
1 parent ab6364d commit 5499af8

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

src/BetterEmbed.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import {MessageAttachment, MessageEmbed} from 'discord.js';
2-
import {AnyObject, CheckSizeContent, CheckSizeKey, Template, TemplatesValues} from './types'
2+
import {AnyObject, CheckSizeContent, CheckSizeKey, Template, TemplatesValues} from './types';
33

44
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-
}
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+
},
3232
};
3333

3434
export const limits = {
@@ -47,9 +47,9 @@ export const limits = {
4747
},
4848
};
4949

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;
50+
export type Templates = typeof templates;
51+
type ValuesFromTemplateKey<T extends string, P = Templates[T]> = P extends Template<infer V> ? V : {};
52+
type Values<T extends string | Template> = T extends Template<infer V> ? V : T extends string ? ValuesFromTemplateKey<T> : AnyObject | undefined;
5353

5454
export class BetterEmbed extends MessageEmbed {
5555
public static LENGTH_LIMITS = limits;
@@ -60,11 +60,14 @@ export class BetterEmbed extends MessageEmbed {
6060
this.checkSize();
6161
}
6262

63-
public static isTemplate(key: string): key is keyof Templates & string {
64-
return templates[key] !== undefined;
65-
}
63+
public static isTemplate(key: string): key is keyof Templates & string {
64+
return templates[key] !== undefined;
65+
}
6666

67-
public static fromTemplate<T extends (keyof Templates & string) | Template, V extends AnyObject | undefined = Values<T>>(template: T, values?: V): BetterEmbed {
67+
public static fromTemplate<T extends (keyof Templates & string) | Template, V extends AnyObject | undefined = Values<T>>(
68+
template: T,
69+
values?: V
70+
): BetterEmbed {
6871
if (typeof template === 'string')
6972
if (templates[template]) template = templates[template] as T;
7073
else throw new Error(`Template '${template}' not found.`);
@@ -182,17 +185,24 @@ export class BetterEmbed extends MessageEmbed {
182185
if (typeof tooLongFields === 'boolean') throw new RangeError(`Too much fields (${limits.fields.size}).`);
183186
else {
184187
const name = 'name' in tooLongFields ? 'value' : 'name';
185-
throw new RangeError(`'embed.fields[${tooLongFields.index}].${name}' is too long: ${this.fields[tooLongFields.index][name].length} (max: ${tooLongFields.limit})`);
188+
throw new RangeError(
189+
`'embed.fields[${tooLongFields.index}].${name}' is too long: ${this.fields[tooLongFields.index][name].length} (max: ${
190+
tooLongFields.limit
191+
})`
192+
);
186193
}
187194
}
188195
}
189196

190197
if (this.title && this.title.length > limits.title) throw new RangeError(`'embed.title' is too long: ${this.title.length} (max: ${limits.title}).`);
191-
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}).`);
192-
if (this.description && this.description.length > limits.description) throw new RangeError(`'embed.description' is too long: ${this.description.length} (max: ${limits.description}).`);
198+
if (this.author?.name && this.author.name.length > limits.author.name)
199+
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)
201+
throw new RangeError(`'embed.description' is too long: ${this.description.length} (max: ${limits.description}).`);
193202
if (this.fields?.length > limits.fields.size) throw new RangeError(`Too much fields (${limits.fields.size}).`);
194203
this.fields.forEach(field => {
195-
if (field.name?.length > limits.fields.name) throw new RangeError(`'embed.fields[${this.fields.indexOf(field)}].name' is too long: ${field.name.length} (max: ${limits.fields.name}).`);
204+
if (field.name?.length > limits.fields.name)
205+
throw new RangeError(`'embed.fields[${this.fields.indexOf(field)}].name' is too long: ${field.name.length} (max: ${limits.fields.name}).`);
196206
if (field.value?.length > limits.fields.value)
197207
throw new RangeError(`'embed.fields[${this.fields.indexOf(field)}].value' is too long: ${field.value.length} (max: ${limits.fields.value}).`);
198208
});

0 commit comments

Comments
 (0)