-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathembedMessages.js
441 lines (381 loc) · 13.3 KB
/
embedMessages.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
module.exports = async function ({ config, bot, formats }) {
const moment = require("moment");
const pluginVersion = "1.1.2";
const KEY = "em";
const truthyValues = ["on", "1", "true"];
const falsyValues = ["off", "0", "false", "null"];
const pfpMap = new Map();
let pfpMapResetTimeout;
function log(message) {
console.log(`[EmbedMessages] ${message}`);
}
// Accepts 100,100,100 and 100 100 100
const isRgb = /^(\d{1,3})\D+(\d{1,3})\D+(\d{1,3})$/;
const mentionRegex = /<@.?(\d{17,19})>/g;
/**
* Parses a color from the input string. The following formats are accepted:
* - #HEXVALUE
* - rrr, ggg, bbb
* - rrr ggg bbb
* @return Parsed color as integer or `null` if no color could be parsed
*/
function parseColor(input) {
// Convert HEX to RGB
if (input.startsWith("#")) {
let r = 0,
g = 0,
b = 0;
// 3 digits
if (input.length == 4) {
r = "0x" + input[1] + input[1];
g = "0x" + input[2] + input[2];
b = "0x" + input[3] + input[3];
// 6 digits
} else if (input.length == 7) {
r = "0x" + input[1] + input[2];
g = "0x" + input[3] + input[4];
b = "0x" + input[5] + input[6];
}
input = `${+r}, ${+g}, ${+b}`;
}
// Convert RGB to INT or return null if invalid
const rgbMatch = input.match(isRgb);
if (rgbMatch) {
const r = parseInt(rgbMatch[1], 10);
const g = parseInt(rgbMatch[2], 10);
const b = parseInt(rgbMatch[3], 10);
if (r > 255 || g > 255 || b > 255) {
return null;
}
// Convert to int and return
return (r << 16) + (g << 8) + b;
}
return null;
}
/**
* Parses a boolean from the input string.
* String must be either truthy or falsy to return boolean
* @return Parsed boolean or `null` if string is neither truthy nor falsy
*/
function parseCustomBoolean(input) {
if (typeof input === "boolean") {
return input;
}
if (truthyValues.includes(input)) return true;
if (falsyValues.includes(input)) return false;
return null;
}
const SETTING_NAMES = Object.freeze({
// Staff -> User
STAFF_REPLY_DM_ENABLED: "staffReplyDmEnabled",
STAFF_REPLY_DM_COLOR: "staffReplyDmColor",
STAFF_REPLY_THREAD_ENABLED: "staffReplyThreadEnabled",
STAFF_REPLY_THREAD_COLOR: "staffReplyThreadColor",
STAFF_REPLY_DM_TIMESTAMP_ENABLE: "staffReplyDmTimestampEnabled",
// User -> Staff
USER_REPLY_THREAD_ENABLED: "userReplyThreadEnabled",
USER_REPLY_THREAD_COLOR: "userReplyThreadColor",
// System -> Any
SYSTEM_USER_DM_ENABLED: "systemReplyDmEnabled",
SYSTEM_USER_DM_COLOR: "systemReplyDmColor",
SYSTEM_USER_THREAD_ENABLED: "systemReplyThreadEnabled",
SYSTEM_USER_THREAD_COLOR: "systemReplyThreadColor",
SYSTEM_STAFF_ENABLED: "systemStaffEnabled",
SYSTEM_STAFF_COLOR: "systemStaffColor",
// Miscellaneous Settings
CUSTOM_SYSTEM_NAME: "systemName",
});
// Init with defaults
const settings = new Map([
// Staff -> User
[SETTING_NAMES.STAFF_REPLY_DM_ENABLED, true],
[SETTING_NAMES.STAFF_REPLY_DM_COLOR, parseColor("#2ECC71")],
[SETTING_NAMES.STAFF_REPLY_THREAD_ENABLED, true],
[SETTING_NAMES.STAFF_REPLY_THREAD_COLOR, parseColor("#2ECC71")],
[SETTING_NAMES.STAFF_REPLY_DM_TIMESTAMP_ENABLE, true],
// User -> Staff
[SETTING_NAMES.USER_REPLY_THREAD_ENABLED, true],
[SETTING_NAMES.USER_REPLY_THREAD_COLOR, parseColor("#9C32A8")],
// System -> Any
[SETTING_NAMES.SYSTEM_USER_DM_ENABLED, true],
[SETTING_NAMES.SYSTEM_USER_DM_COLOR, parseColor("#5865F2")],
[SETTING_NAMES.SYSTEM_USER_THREAD_ENABLED, true],
[SETTING_NAMES.SYSTEM_USER_THREAD_COLOR, parseColor("#5865F2")],
[SETTING_NAMES.SYSTEM_STAFF_ENABLED, true],
[SETTING_NAMES.SYSTEM_STAFF_COLOR, parseColor("#1AA4BC")],
// Miscellaneous Settings
[SETTING_NAMES.CUSTOM_SYSTEM_NAME, "System"],
]);
// Load config settings
if (KEY in config) {
for (const [name, override] of Object.entries(config.em)) {
if (!settings.has(name)) {
log(`Setting ${name} is not a valid setting`);
}
if (name.toLowerCase().includes("enabled")) {
const parsedBool = parseCustomBoolean(override);
if (parsedBool === null) {
log(`Value ${override} is not a valid truthy or falsy value`);
} else {
settings.set(name, parsedBool);
}
} else if (name.toLowerCase().includes("color")) {
const parsedColor = parseColor(override);
if (!parsedColor) {
log(`Value ${override} is not a valid RGB or HEX color`);
} else {
settings.set(name, parsedColor);
}
} else {
settings.set(name, override);
}
}
}
// Do auto-value checks on boot instead of once every message
const systemName = settings.get(SETTING_NAMES.CUSTOM_SYSTEM_NAME).toLowerCase() === "$botname" ? bot.user.username : settings.get(SETTING_NAMES.CUSTOM_SYSTEM_NAME);
/**
* Returns pfp url for userId
* If the userId is in our internal cache, return it
* If it is not, search through bot's cached users and store it
*
* This should make pfp retrieval quicker on large servers where
* using find on the whole cache could take quite long
* @param {*} userId
* @returns
*/
function getPfp(userId) {
let pfp = null;
if (pfpMap.has(userId)) {
pfp = pfpMap.get(userId);
} else {
pfp = bot.users.find((x) => x.id === userId).avatarURL;
pfpMap.set(userId, pfp);
}
return pfp;
}
const replyToUserFormatter = function (threadMessage) {
const userId = threadMessage.user_id;
const roleName = threadMessage.role_name || config.fallbackRoleName || "";
const embed = { description: threadMessage.body, color: settings.get(SETTING_NAMES.STAFF_REPLY_DM_COLOR) };
if (!threadMessage.is_anonymous) {
embed.author = {
name: `${threadMessage.user_name} ${roleName != "" ? `(${roleName})` : ""}`.trim(),
icon_url: getPfp(userId),
};
} else {
embed.author = {
name: roleName,
icon_url: bot.user.avatarURL,
};
}
if (threadMessage.attachments.length === 1) {
if (
threadMessage.attachments[0].endsWith(".png") ||
threadMessage.attachments[0].endsWith(".jpg") ||
threadMessage.attachments[0].endsWith(".gif")
) {
embed.image = {
url: threadMessage.attachments[0],
};
} else {
embed.description += `\n${threadMessage.attachments[0]}`;
}
} else {
for (const link of threadMessage.attachments) {
embed.description += `\n${link}`;
}
}
if (config.threadTimestamps && settings.get(SETTING_NAMES.STAFF_REPLY_DM_TIMESTAMP_ENABLE)) {
embed.timestamp = moment().utc().toISOString();
}
return { embed };
};
const replyInThreadFormatter = function (threadMessage) {
const userId = threadMessage.user_id;
const roleName = threadMessage.role_name || config.fallbackRoleName || "";
const embed = {
description: threadMessage.body,
color: settings.get(SETTING_NAMES.STAFF_REPLY_THREAD_COLOR),
footer: { text: `#${threadMessage.message_number}` },
};
if (!threadMessage.is_anonymous) {
embed.author = {
name: `${threadMessage.user_name} ${roleName != "" ? `(${roleName})` : ""}`.trim(),
icon_url: getPfp(userId),
};
} else {
embed.author = {
name: `${roleName} (${threadMessage.user_name})`.trim(),
icon_url: bot.user.avatarURL,
};
}
if (threadMessage.attachments.length === 1) {
if (
threadMessage.attachments[0].endsWith(".png") ||
threadMessage.attachments[0].endsWith(".jpg") ||
threadMessage.attachments[0].endsWith(".gif")
) {
embed.image = {
url: threadMessage.attachments[0],
};
} else {
embed.description += `\n${threadMessage.attachments[0]}`;
}
} else {
for (const link of threadMessage.attachments) {
embed.description += `\n${link}`;
}
}
if (config.threadTimestamps) {
embed.timestamp = moment().utc().toISOString();
}
return { embed };
};
const userReplyFormatter = function (threadMessage) {
const userId = threadMessage.user_id;
const embed = { description: threadMessage.body, color: settings.get(SETTING_NAMES.USER_REPLY_THREAD_COLOR) };
embed.author = {
name: `${threadMessage.user_name}`,
icon_url: getPfp(userId),
};
if (threadMessage.attachments.length === 1) {
if (
threadMessage.attachments[0].endsWith(".png") ||
threadMessage.attachments[0].endsWith(".jpg") ||
threadMessage.attachments[0].endsWith(".gif")
) {
embed.image = {
url: threadMessage.attachments[0],
};
} else {
embed.description += `\n${threadMessage.attachments[0]}`;
}
} else {
for (const link of threadMessage.attachments) {
embed.description += `\n${link}`;
}
}
if (config.threadTimestamps) {
embed.timestamp = moment().utc().toISOString();
}
return { embed };
};
const systemToUserDmFormatter = function (threadMessage) {
const embed = { description: threadMessage.body, color: settings.get(SETTING_NAMES.SYSTEM_USER_DM_COLOR) };
embed.author = {
name: systemName,
icon_url: bot.user.avatarURL,
};
if (threadMessage.attachments.length === 1) {
if (
threadMessage.attachments[0].endsWith(".png") ||
threadMessage.attachments[0].endsWith(".jpg") ||
threadMessage.attachments[0].endsWith(".gif")
) {
embed.image = {
url: threadMessage.attachments[0],
};
} else {
embed.description += `\n${threadMessage.attachments[0]}`;
}
} else {
for (const link of threadMessage.attachments) {
embed.description += `\n${link}`;
}
}
if (config.threadTimestamps && settings.get(SETTING_NAMES.STAFF_REPLY_DM_TIMESTAMP_ENABLE)) {
embed.timestamp = moment().utc().toISOString();
}
return { embed };
};
const systemToUserThreadFormatter = function (threadMessage) {
const embed = { description: threadMessage.body, color: settings.get(SETTING_NAMES.SYSTEM_USER_THREAD_COLOR) };
embed.author = {
name: systemName,
icon_url: bot.user.avatarURL,
};
if (threadMessage.attachments.length === 1) {
if (
threadMessage.attachments[0].endsWith(".png") ||
threadMessage.attachments[0].endsWith(".jpg") ||
threadMessage.attachments[0].endsWith(".gif")
) {
embed.image = {
url: threadMessage.attachments[0],
};
} else {
embed.description += `\n${threadMessage.attachments[0]}`;
}
} else {
for (const link of threadMessage.attachments) {
embed.description += `\n${link}`;
}
}
if (config.threadTimestamps) {
embed.timestamp = moment().utc().toISOString();
}
return { embed };
};
const systemToStaffFormatter = function (threadMessage) {
const embed = { description: threadMessage.body, color: settings.get(SETTING_NAMES.SYSTEM_STAFF_COLOR) };
embed.author = {
name: systemName,
icon_url: bot.user.avatarURL,
};
if (threadMessage.attachments.length === 1) {
if (
threadMessage.attachments[0].endsWith(".png") ||
threadMessage.attachments[0].endsWith(".jpg") ||
threadMessage.attachments[0].endsWith(".gif")
) {
embed.image = {
url: threadMessage.attachments[0],
};
} else {
embed.description += `\n${threadMessage.attachments[0]}`;
}
} else {
for (const link of threadMessage.attachments) {
embed.description += `\n${link}`;
}
}
// We can't directly join the matched array since that results in "@Dark,108552944961454080"
const foundMentions = threadMessage.body.matchAll(mentionRegex);
const properMentions = [];
for (const men of foundMentions) {
properMentions.push(men[0]);
}
if (config.threadTimestamps) {
embed.timestamp = moment().utc().toISOString();
}
return { content: properMentions.join(" "), embed, allowedMentions: {users: true, roles: true, everyone: true} };
};
// Reset the pfpMap every hour or so, we dont want outdated pfp's to stay forever
function resetPfpMap() {
pfpMap.clear();
pfpMapResetTimeout = setTimeout(resetPfpMap, 1000 * 60 * 60);
}
resetPfpMap();
//#region registering
// Register all formatters
if (settings.get(SETTING_NAMES.STAFF_REPLY_DM_ENABLED)) {
formats.setStaffReplyDMFormatter(replyToUserFormatter);
}
if (settings.get(SETTING_NAMES.STAFF_REPLY_THREAD_ENABLED)) {
formats.setStaffReplyThreadMessageFormatter(replyInThreadFormatter);
}
if (settings.get(SETTING_NAMES.USER_REPLY_THREAD_ENABLED)) {
formats.setUserReplyThreadMessageFormatter(userReplyFormatter);
}
if (settings.get(SETTING_NAMES.SYSTEM_USER_DM_ENABLED)) {
formats.setSystemToUserDMFormatter(systemToUserDmFormatter);
}
if (settings.get(SETTING_NAMES.SYSTEM_USER_THREAD_ENABLED)) {
formats.setSystemToUserThreadMessageFormatter(systemToUserThreadFormatter);
}
if (settings.get(SETTING_NAMES.SYSTEM_STAFF_ENABLED)) {
formats.setSystemThreadMessageFormatter(systemToStaffFormatter);
}
//#endregion
log(`Version ${pluginVersion} loaded`);
};