Skip to content

Commit a4a5505

Browse files
committed
Refactored exports.
1 parent 79bdc2d commit a4a5505

File tree

4 files changed

+270
-36
lines changed

4 files changed

+270
-36
lines changed

dist/BetterEmbed.d.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { MessageEmbed, MessageEmbedOptions } from 'discord.js';
2+
declare type AnyObject = {
3+
[k: string]: any;
4+
};
5+
declare const templates: {
6+
basic: {
7+
footer: {
8+
text: string;
9+
iconURL: string;
10+
};
11+
timestamp: Date;
12+
};
13+
color: {
14+
color: string;
15+
};
16+
readonly complete: {
17+
title: string;
18+
description: string;
19+
color: string;
20+
footer: {
21+
text: string;
22+
iconURL: string;
23+
};
24+
timestamp: Date;
25+
};
26+
readonly image: {
27+
image: {
28+
url: string;
29+
};
30+
title: string;
31+
description: string;
32+
color: string;
33+
footer: {
34+
text: string;
35+
iconURL: string;
36+
};
37+
timestamp: Date;
38+
};
39+
};
40+
declare class BetterEmbed extends MessageEmbed {
41+
static fromTemplate(template: keyof typeof templates | typeof templates | MessageEmbedOptions, values: AnyObject): BetterEmbed;
42+
constructor(data?: MessageEmbed | MessageEmbedOptions);
43+
checkSize(): void;
44+
cutIfTooLong(): void;
45+
}
46+
declare const _default: {
47+
BetterEmbed: typeof BetterEmbed;
48+
templates: {
49+
basic: {
50+
footer: {
51+
text: string;
52+
iconURL: string;
53+
};
54+
timestamp: Date;
55+
};
56+
color: {
57+
color: string;
58+
};
59+
readonly complete: {
60+
title: string;
61+
description: string;
62+
color: string;
63+
footer: {
64+
text: string;
65+
iconURL: string;
66+
};
67+
timestamp: Date;
68+
};
69+
readonly image: {
70+
image: {
71+
url: string;
72+
};
73+
title: string;
74+
description: string;
75+
color: string;
76+
footer: {
77+
text: string;
78+
iconURL: string;
79+
};
80+
timestamp: Date;
81+
};
82+
};
83+
limits: {
84+
author: {
85+
name: number;
86+
};
87+
title: number;
88+
description: number;
89+
footer: {
90+
text: number;
91+
};
92+
fields: {
93+
size: number;
94+
name: number;
95+
value: number;
96+
};
97+
};
98+
};
99+
export default _default;

dist/BetterEmbed.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const discord_js_1 = require("discord.js");
4+
const templates = {
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+
get complete() {
16+
return {
17+
...this.basic,
18+
...this.color,
19+
title: '${title}',
20+
description: '${description}',
21+
};
22+
},
23+
get image() {
24+
return {
25+
...this.complete,
26+
image: {
27+
url: '${image}',
28+
},
29+
};
30+
},
31+
};
32+
const limits = {
33+
author: {
34+
name: 256,
35+
},
36+
title: 256,
37+
description: 2048,
38+
footer: {
39+
text: 2048,
40+
},
41+
fields: {
42+
size: 25,
43+
name: 256,
44+
value: 1024,
45+
},
46+
};
47+
class BetterEmbed extends discord_js_1.MessageEmbed {
48+
static fromTemplate(template, values) {
49+
if (typeof template === 'string')
50+
if (templates[template])
51+
template = templates[template];
52+
else
53+
throw new Error(`Template '${template}' not found.`);
54+
template = JSON.parse(JSON.stringify(template));
55+
function setValues(object, values) {
56+
for (const [name, value] of Object.entries(object)) {
57+
if (!object.hasOwnProperty(name))
58+
continue;
59+
if (Array.isArray(value))
60+
object[name] = value.map(v => setValues(v, values));
61+
if (typeof value === 'object') {
62+
object[name] = setValues(value, values);
63+
continue;
64+
}
65+
const code = value.replace(/\$\{([^}]+)\}/gu, (_, value) => (values.hasOwnProperty(value.split('.')[0]) ? `\${values.${value}}` : value));
66+
object[name] = eval(`\`${code}\``);
67+
}
68+
return object;
69+
}
70+
return new BetterEmbed(setValues(template, values));
71+
}
72+
constructor(data) {
73+
super(data);
74+
this.checkSize();
75+
}
76+
checkSize() {
77+
if (this.title && this.title.length > limits.title)
78+
throw new RangeError(`embed.title is too long (${limits.title}).`);
79+
if (this.author?.name && this.author.name.length > limits.author.name)
80+
throw new RangeError(`embed.author.name is too long (${limits.author.name}).`);
81+
if (this.description && this.description.length > limits.description)
82+
throw new RangeError(`embed.description is too long (${limits.description}).`);
83+
if (this.title && this.title.length > limits.title)
84+
throw new RangeError(`embed.title is too long (${limits.title}).`);
85+
if (this.fields?.length > limits.fields.size)
86+
throw new RangeError(`Too much fields is too long (${limits.fields.size}).`);
87+
this.fields.forEach(field => {
88+
if (field.name?.length > limits.fields.name)
89+
throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].name is too long (${limits.fields.name}).`);
90+
if (field.value?.length > limits.fields.value)
91+
throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].value is too long (${limits.fields.value}).`);
92+
});
93+
}
94+
cutIfTooLong() {
95+
function cutWithLength(text, maxLength) {
96+
return text.length > maxLength ? `${text.substring(0, maxLength - 3)}...` : text;
97+
}
98+
if (this.author?.name)
99+
this.author.name = cutWithLength(this.author.name, limits.author.name);
100+
if (this.description)
101+
this.description = cutWithLength(this.description, limits.description);
102+
if (this.title)
103+
this.title = cutWithLength(this.title, limits.title);
104+
if (this.fields) {
105+
if (this.fields.length > limits.fields.size)
106+
this.fields = this.fields.slice(0, limits.fields.size) ?? [];
107+
this.fields.forEach(field => {
108+
field.name = cutWithLength(field.name ?? '', limits.fields.name);
109+
field.value = cutWithLength(field.value ?? '', limits.fields.value);
110+
});
111+
}
112+
}
113+
}
114+
exports.default = {
115+
BetterEmbed,
116+
templates,
117+
limits
118+
};

dist/t.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const {BetterEmbed} = require('./BetterEmbed.js');
2+
console.log(BetterEmbed.fromTemplate('color'));

src/BetterEmbed.ts

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {MessageEmbed, MessageEmbedOptions} from 'discord.js';
22

3-
type AnyObject = { [k: string] : any};
3+
type AnyObject = { [k: string]: any };
44

55
const templates = {
66
basic: {
@@ -31,22 +31,28 @@ const templates = {
3131
},
3232
};
3333

34-
export default class BetterEmbed extends MessageEmbed {
35-
public static limits = {
36-
author: {
37-
name: 256,
38-
},
39-
title: 256,
40-
description: 2048,
41-
footer: {
42-
text: 2048,
43-
},
44-
fields: {
45-
size: 25,
46-
name: 256,
47-
value: 1024,
48-
},
49-
};
34+
const limits = {
35+
author: {
36+
name: 256,
37+
},
38+
title: 256,
39+
description: 2048,
40+
footer: {
41+
text: 2048,
42+
},
43+
fields: {
44+
size: 25,
45+
name: 256,
46+
value: 1024,
47+
},
48+
};
49+
50+
class BetterEmbed extends MessageEmbed {
51+
52+
public constructor(data?: MessageEmbed | MessageEmbedOptions) {
53+
super(data);
54+
this.checkSize();
55+
}
5056

5157
static fromTemplate(template: keyof typeof templates | typeof templates | MessageEmbedOptions, values: AnyObject) {
5258
if (typeof template === 'string')
@@ -64,7 +70,9 @@ export default class BetterEmbed extends MessageEmbed {
6470
continue;
6571
}
6672

67-
const code = value.replace(/\$\{([^}]+)\}/gu, (_: any, value: string) => (values.hasOwnProperty(value.split('.')[0]) ? `\${values.${value}}` : value));
73+
const code = value.replace(/\$\{([^}]+)\}/gu, (_: any, value: string) => (values.hasOwnProperty(value.split('.')[0])
74+
? `\${values.${value}}`
75+
: value));
6876
object[name] = eval(`\`${code}\``);
6977
}
7078

@@ -74,20 +82,15 @@ export default class BetterEmbed extends MessageEmbed {
7482
return new BetterEmbed(setValues(template as AnyObject, values));
7583
}
7684

77-
public constructor(data?: MessageEmbed | MessageEmbedOptions) {
78-
super(data);
79-
this.checkSize();
80-
}
81-
8285
checkSize() {
83-
if (this.title && this.title.length > BetterEmbed.limits.title) throw new RangeError(`embed.title is too long (${BetterEmbed.limits.title}).`);
84-
if (this.author?.name && this.author.name.length > BetterEmbed.limits.author.name) throw new RangeError(`embed.author.name is too long (${BetterEmbed.limits.author.name}).`);
85-
if (this.description && this.description.length > BetterEmbed.limits.description) throw new RangeError(`embed.description is too long (${BetterEmbed.limits.description}).`);
86-
if (this.title && this.title.length > BetterEmbed.limits.title) throw new RangeError(`embed.title is too long (${BetterEmbed.limits.title}).`);
87-
if (this.fields?.length > BetterEmbed.limits.fields.size) throw new RangeError(`Too much fields is too long (${BetterEmbed.limits.fields.size}).`);
86+
if (this.title && this.title.length > limits.title) throw new RangeError(`embed.title is too long (${limits.title}).`);
87+
if (this.author?.name && this.author.name.length > limits.author.name) throw new RangeError(`embed.author.name is too long (${limits.author.name}).`);
88+
if (this.description && this.description.length > limits.description) throw new RangeError(`embed.description is too long (${limits.description}).`);
89+
if (this.title && this.title.length > limits.title) throw new RangeError(`embed.title is too long (${limits.title}).`);
90+
if (this.fields?.length > limits.fields.size) throw new RangeError(`Too much fields is too long (${limits.fields.size}).`);
8891
this.fields.forEach(field => {
89-
if (field.name?.length > BetterEmbed.limits.fields.name) throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].name is too long (${BetterEmbed.limits.fields.name}).`);
90-
if (field.value?.length > BetterEmbed.limits.fields.value) throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].value is too long (${BetterEmbed.limits.fields.value}).`);
92+
if (field.name?.length > limits.fields.name) throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].name is too long (${limits.fields.name}).`);
93+
if (field.value?.length > limits.fields.value) throw new RangeError(`embed.fields[${this.fields.indexOf(field)}].value is too long (${limits.fields.value}).`);
9194
});
9295
}
9396

@@ -96,15 +99,27 @@ export default class BetterEmbed extends MessageEmbed {
9699
return text.length > maxLength ? `${text.substring(0, maxLength - 3)}...` : text;
97100
}
98101

99-
if (this.author?.name) this.author.name = cutWithLength(this.author.name, BetterEmbed.limits.author.name);
100-
if(this.description) this.description = cutWithLength(this.description, BetterEmbed.limits.description);
101-
if(this.title) this.title = cutWithLength(this.title, BetterEmbed.limits.title);
102+
if (this.author?.name) this.author.name = cutWithLength(this.author.name, limits.author.name);
103+
if (this.description) this.description = cutWithLength(this.description, limits.description);
104+
if (this.title) this.title = cutWithLength(this.title, limits.title);
102105
if (this.fields) {
103-
if (this.fields.length > BetterEmbed.limits.fields.size) this.fields = this.fields.slice(0, BetterEmbed.limits.fields.size) ?? [];
106+
if (this.fields.length > limits.fields.size) this.fields = this.fields.slice(0, limits.fields.size) ?? [];
104107
this.fields.forEach(field => {
105-
field.name = cutWithLength(field.name ?? '', BetterEmbed.limits.fields.name);
106-
field.value = cutWithLength(field.value ?? '', BetterEmbed.limits.fields.value);
108+
field.name = cutWithLength(field.name ?? '', limits.fields.name);
109+
field.value = cutWithLength(field.value ?? '', limits.fields.value);
107110
});
108111
}
109112
}
110113
}
114+
115+
export default {
116+
BetterEmbed,
117+
templates,
118+
limits,
119+
};
120+
121+
module.exports = {
122+
BetterEmbed,
123+
templates,
124+
limits,
125+
};

0 commit comments

Comments
 (0)