-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.ts
459 lines (402 loc) · 11.2 KB
/
utils.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* discord-dataming - powerful discord datamining, datamines discord
* Copyright (C) 2023 Jozef Steinhübl
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* **/
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import {
mkdir,
writeFile as nodeWriteFile,
rm as nodeRm,
readFile as nodeReadFile,
access as nodeAccess,
constants,
} from "node:fs/promises";
import { $ } from "bun";
import simpleGit from "simple-git";
import { REST } from "@discordjs/rest";
import { Octokit } from "@octokit/rest";
import deepEqual from "fast-deep-equal";
import { setTimeout as sleep } from "node:timers/promises";
import { Routes, type APIEmbed, ButtonStyle } from "discord-api-types/v10";
import { ActionRowBuilder, ButtonBuilder } from "@discordjs/builders";
import prettier from "prettier";
export const __dirname = fileURLToPath(new URL(".", import.meta.url));
export const DATA_DIR = join(__dirname, "..", "data");
export const git = simpleGit();
export const discordRest = new REST({ version: "10" }).setToken(
process.env.DISCORD_TOKEN ?? "",
);
export const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN ?? "",
});
export const pushToGit = async (...message: string[]) => {
const msg: string[] = [];
for (const m of message) {
msg.push(maximumStringLen(m, 20000));
}
if (process.env.DEBUG === "true") {
console.log(`Avoiding ${msg} push`);
return; /*{
update: {
hash: "dev-debug",
},
};*/
}
console.log(await $`git add data/*`);
const text =
await $`git commit ${{ raw: msg.map((m) => `-m "${m}"`).join(" ") }}`
.throws(false)
.text();
if (text.includes("Your branch is up to date with")) {
console.log("Up to date!!");
return;
}
try {
return await git.push();
} catch (e) {
console.log(e);
console.log(await $`git stash`);
const text = await $`git pull`.throws(false).text();
if (text.includes("You can instead skip this commit")) {
console.log(await $`git rebase --skip`);
}
console.log(await $`git stash pop`);
return await git.push();
}
};
export const postToGithub = async (commitHash: string, body: string) => {
return await octokit.repos.createCommitComment({
owner: "xHyroM",
repo: "discord-datamining",
commit_sha: commitHash,
body: maximumStringLen(body, 65530),
});
};
interface Webhooks {
name: string;
urls: string[];
}
export const postToDiscord = async (
webhooks: Webhooks,
pushHash: string | undefined,
body: {
content?: string;
embeds?: APIEmbed[];
},
url?: string,
) => {
for (const webhook of webhooks.urls) {
const [id, token] = webhook.split("/").slice(-2);
try {
await discordRest.post(Routes.webhook(id!, token!), {
body: {
components: [
new ActionRowBuilder()
.setComponents(
new ButtonBuilder()
.setLabel("View on GitHub")
.setStyle(ButtonStyle.Link)
.setURL(
url
? url
: pushHash
? `https://github.com/xHyroM/discord-datamining/commit/${pushHash}`
: "https://github.com/xHyroM/discord-datamining",
),
)
.toJSON(),
],
...body,
allowed_mentions: {
parse: ["roles"],
},
},
});
} catch (e) {
if (process.env.DEBUG === "true") console.log(e);
console.log(
"Failed to push ",
webhooks.name,
e.toString().replaceAll(id, "").replaceAll(token, ""),
);
continue;
}
}
};
export const getWebhookFromEnv = (name: string): Webhooks => {
const webhook =
process.env[process.env.IS_DEV ? "DISCORD_WEBHOOK_DEV" : name];
if (!webhook) throw new Error(`Missing webhook: ${name}`);
try {
return {
name,
urls: JSON.parse(webhook) as string[],
};
} catch {
return {
name,
urls: [webhook],
};
}
};
export const getPaginator = async (
url: string,
dataField: any,
page: number,
partial?: any[],
): Promise<any[]> => {
if (!partial) partial = [];
const res = await fetch(`${url}?page=${page}&per_page=100`);
// check for ratelimit
if (res.status === 429) {
const body = await res.json();
const retryAfter = body.retry_after;
console.log(`[paginator] 429: Waiting ${retryAfter}ms`);
await sleep(retryAfter + 1000);
return await getPaginator(url, dataField, page);
}
if (!res.ok) return partial;
const json = await res.json();
if (json[dataField]) partial.push(...json[dataField]);
return json[dataField].length > 0
? getPaginator(url, dataField, page + 1, partial)
: partial;
};
export const omit = <T, K extends keyof T>(
obj: T,
...keys: (keyof T)[]
): Omit<T, K> => {
const newObj = { ...obj };
for (const key of keys) delete newObj[key];
return newObj;
};
export const omitEndsWith = <T extends Record<string, any>>(
obj: T,
...keys: string[]
) => {
const newObj = { ...obj };
for (const key of keys) {
for (const k of Object.keys(newObj)) {
if (k.endsWith(key)) delete newObj[k];
}
}
return newObj;
};
export const exist = async (path: string) => {
try {
await nodeAccess(path, constants.F_OK);
return true;
} catch {
return false;
}
};
export const readFile = async (path: string) => {
if (!(await exist(path))) return null;
return await nodeReadFile(path, "utf8");
};
export const writeFile = async (path: string, data: string) => {
const dirs =
process.platform === "linux"
? path.split("/").slice(0, -1)
: path.split("\\").slice(0, -1);
await mkdir(dirs.join("/"), { recursive: true });
await nodeWriteFile(path, data);
};
export const rm = async (path: string) => {
try {
await nodeRm(path, {
recursive: true,
force: true,
});
} catch {}
};
export const beautify = async (
content: string,
type: "js" | "css" | "html",
): Promise<string> => {
switch (type) {
case "js":
return await prettier.format(content, {
parser: "babel",
});
case "css":
return await prettier.format(content, {
parser: "css",
});
case "html":
return await prettier.format(content, {
parser: "html",
});
}
};
export const formatNumber = (number: number) => {
return number.toLocaleString("en-US");
};
export const maximumStringLen = (str: string, max: number) => {
if (str.startsWith("```") && str.endsWith("```")) {
return str.length > max ? str.slice(0, max - 6) + "...```" : str;
}
return str.length > max ? str.slice(0, max - 3) + "..." : str;
};
export const chunk = <T>(array: T[], size: number): T[][] => {
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
};
export const numberPad = (num: number): string => {
if (num < 10) {
return `0${num}`;
}
return num.toString();
};
export async function retry<T>(
fn: () => Promise<T>,
retries: number,
timeout = 1000,
onRetry?: () => Promise<void>,
): Promise<T> {
try {
return await fn();
} catch (err) {
if (retries <= 0) {
throw err;
}
if (onRetry) {
await onRetry();
}
await new Promise((resolve) => setTimeout(resolve, timeout));
return retry(fn, retries - 1, timeout, onRetry);
}
}
export interface ArrayDiff<T> {
added: T[];
removed: T[];
updated: {
new: T;
old: T;
changes: Record<
string,
{ new: string | number | boolean; old: string | number | boolean }
>;
}[];
}
export function createDiff<T extends Record<string, any>>(
a: T[],
b: T[],
compareKey: keyof T,
updateIgnoreKeys: (keyof T)[] = [],
): ArrayDiff<T> {
const added: T[] = [];
const removed: T[] = [];
const updated: {
new: T;
old: T;
changes: Record<
string,
{ new: string | number | boolean; old: string | number | boolean }
>;
}[] = [];
for (const b1 of b) {
const aItem = a.find((item) => item[compareKey] === b1[compareKey]);
if (!aItem) {
added.push(b1);
} else {
const aItemOmitted = omit(aItem, ...updateIgnoreKeys);
const b1Omitted = omit(b1, ...updateIgnoreKeys);
if (!deepEqual(aItemOmitted, b1Omitted)) {
updated.push({
old: aItem,
new: b1,
changes: getChanges(aItem, b1, updateIgnoreKeys),
});
}
}
}
for (const a1 of a) {
if (!b.some((a) => a[compareKey] === a1[compareKey])) {
removed.push(a1);
}
}
return { added, removed, updated };
}
export function mergeDiffs<T>(...diffs: ArrayDiff<T>[]): ArrayDiff<T> {
const added = [];
const removed = [];
const updated = [];
for (const diff of diffs) {
added.push(...diff.added);
removed.push(...diff.removed);
updated.push(...diff.updated);
}
return { added, removed, updated };
}
function getChanges<T extends Record<string, any>>(
oldObj: T,
newObj: T,
updateIgnoreKeys: (keyof T)[] = [],
path = "",
): Record<
string,
{ new: string | number | boolean; old: string | number | boolean }
> {
const changes: Record<
string,
{ new: string | number | boolean; old: string | number | boolean }
> = {};
if (oldObj == undefined) oldObj = {} as T;
if (newObj == undefined) newObj = {} as T;
for (const key of new Set([...Object.keys(oldObj), ...Object.keys(newObj)])) {
if (updateIgnoreKeys.includes(key as keyof T)) continue;
const newPath = path ? `${path}.${key}` : key;
if (Array.isArray(newObj[key])) {
(newObj[key] as unknown[]).forEach((item, index) => {
if (typeof item === "object" && item !== null) {
Object.assign(
changes,
getChanges(
(oldObj?.[key as keyof T] as T[])?.[index] as T,
item as T,
updateIgnoreKeys,
`${newPath}[${index}]`,
),
);
} else if (!deepEqual((oldObj[key] as unknown[])[index], item)) {
changes[`${newPath}[${index}]`] = {
old: (oldObj[key] as unknown[])[index],
new: item,
};
}
});
} else if (
typeof newObj[key] === "object" &&
newObj[key] !== null &&
!Array.isArray(newObj[key])
) {
Object.assign(
changes,
getChanges(
oldObj?.[key as keyof T] as T,
newObj?.[key as keyof T] as T,
updateIgnoreKeys,
newPath,
),
);
} else if (!deepEqual(oldObj[key], newObj[key])) {
changes[newPath] = { old: oldObj[key], new: newObj[key] };
}
}
return changes;
}