-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
163 lines (144 loc) · 5.13 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { ConfigureEmbedData, EmbedWithFields } from './types';
import { EmbedBuilder } from 'discord.js';
import { AvailableEmbedConfiguration } from './enums';
import { EmbedConstants, StringUtils } from '@rhidium/core';
import { appConfig } from '@/config';
export const fieldIdentificationLength = 6;
export const maxFieldNameLength = EmbedConstants.FIELD_NAME_MAX_LENGTH - fieldIdentificationLength;
export const settingsKeyFromEmbedOption = (
embedOption: AvailableEmbedConfiguration,
) =>
embedOption === AvailableEmbedConfiguration.MEMBER_JOIN
? 'memberJoinEmbed'
: 'memberLeaveEmbed';
export const configureEmbedInputToEmbedData = (options: {
[k: string]: string | undefined | null;
}): {
embed: ConfigureEmbedData,
message: string | null,
} => {
const embedData: ConfigureEmbedData = {
title: options['title'],
color: options['color'],
authorName: options['author-name'],
authorIconUrl: options['author-icon-url'],
authorUrl: options['author-url'],
description: options['description'],
url: options['url'],
imageUrl: options['image-url'],
thumbnailUrl: options['thumbnail-url'],
footerText: options['footer-text'],
footerIconUrl: options['footer-icon-url'],
fields: [],
};
for (let i = 1; i <= EmbedConstants.MAX_FIELDS_LENGTH; i++) {
const field = options[`field-${i}`];
if (!field) continue;
embedData.fields.push(field);
}
return {
embed: embedData,
message: options['message'] ?? null,
};
};
export const embedDataUrlProperties: (keyof ConfigureEmbedData)[] = [
'url',
'imageUrl',
'thumbnailUrl',
'authorIconUrl',
'footerIconUrl',
];
export const filterInvalidUrls = (data: ConfigureEmbedData) => {
const resolvedData = { ...data };
for (const property of embedDataUrlProperties) {
const url = resolvedData[property];
if (!url || typeof url !== 'string') continue;
const isURL = StringUtils.isUrl(url);
if (!isURL) delete resolvedData[property];
}
return resolvedData;
};
/**
* Constructs an EmbedBuilder from the given ConfigureEmbedData,
* and validates and filters out invalid properties, like color and urls
*/
export const resolveConfigureEmbedData = (
data: ConfigureEmbedData,
initialEmbed = new EmbedBuilder(),
): EmbedBuilder => {
const embed = initialEmbed;
const resolvedData = filterInvalidUrls(data);
if (resolvedData.color) embed.setColor(resolvedData.color ? parseInt(resolvedData.color, 16) : null);
if (resolvedData.title || resolvedData.title === null) embed.setTitle(resolvedData.title);
if (resolvedData.description || resolvedData.description === null) embed.setDescription(resolvedData.description);
if (resolvedData.url || resolvedData.url === null) embed.setURL(resolvedData.url);
if (resolvedData.imageUrl || resolvedData.imageUrl === null) embed.setImage(resolvedData.imageUrl);
if (resolvedData.thumbnailUrl || resolvedData.thumbnailUrl === null) embed.setThumbnail(resolvedData.thumbnailUrl);
if (resolvedData.authorName === null) embed.setAuthor(null);
else if (resolvedData.authorName) {
const author: {
name: string;
iconURL?: string;
url?: string;
} = { name: resolvedData.authorName };
if (resolvedData.authorIconUrl) author.iconURL = resolvedData.authorIconUrl;
if (resolvedData.authorUrl) author.url = resolvedData.authorUrl;
embed.setAuthor(author);
}
if (resolvedData.footerText === null) embed.setFooter(null);
else if (resolvedData.footerText) {
const footer: {
text: string;
iconURL?: string;
} = { text: resolvedData.footerText };
if (resolvedData.footerIconUrl) footer.iconURL = resolvedData.footerIconUrl;
embed.setFooter(footer);
}
for (const field of resolvedData.fields) {
const [name, value, inline] = field.split(';');
if (!name || !value) continue;
embed.addFields({
name,
value,
inline: inline === 'true',
});
}
return embed;
};
export const embedFromEmbedModel = (
embed: EmbedWithFields | null,
baseEmbed = new EmbedBuilder(),
) => {
const embedBuilder = baseEmbed.setColor(embed?.color ?? appConfig.colors.primary);
if (embed?.title) embedBuilder.setTitle(embed.title);
if (embed?.description) embedBuilder.setDescription(embed.description);
if (embed?.url) embedBuilder.setURL(embed.url);
if (embed?.imageURL) embedBuilder.setImage(embed.imageURL);
if (embed?.thumbnailURL) embedBuilder.setThumbnail(embed.thumbnailURL);
if (embed?.authorName) {
const author: {
name: string;
icon_url?: string;
url?: string;
} = { name: embed?.authorName };
if (embed?.authorIconURL) author.icon_url = embed.authorIconURL;
if (embed?.authorURL) author.url = embed.authorURL;
embedBuilder.setAuthor(author);
}
if (embed?.footerText) {
const footer: {
text: string;
icon_url?: string;
} = { text: embed?.footerText };
if (embed?.footerIconURL) footer.icon_url = embed.footerIconURL;
embedBuilder.setFooter(footer);
}
if (embed?.fields) for (const field of embed.fields) {
embedBuilder.addFields({
name: field.name,
value: field.value,
inline: field.inline,
});
}
return embedBuilder;
};