-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateInvites.ts
More file actions
81 lines (77 loc) · 2.32 KB
/
generateInvites.ts
File metadata and controls
81 lines (77 loc) · 2.32 KB
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
import { REST } from "@discordjs/rest";
import { Routes, type APIInvite } from "discord-api-types/v10";
import { inspect } from "util";
import type { Plugin } from "vite";
// name is just for identification purposes
const featuredInvites: { name: string; code: string }[] = [
{
name: "Booney",
code: "sikky",
},
{
name: "moose",
code: "83JRy5KWrY",
},
{
name: "Hi Anime",
code: "hianime",
},
{
name: "Sanctuary, Diablo 2 Trading & Community",
code: "d2r",
},
{
name: "Art of Poetry",
code: "poetry",
},
{
name: "Block by Block (MC Story Mode thing)",
code: "blockbyblock",
},
{
name: "Sailorpiece (Roblox)",
code: "sailorpiece",
},
];
export default function generateInvitesFile(): Plugin {
return {
name: "generate-invites-file",
async buildStart(this, options) {
const BOT_TOKEN = process.env.BOT_TOKEN as string | undefined;
if (!BOT_TOKEN) {
console.error("[WARN] Bot token not configured");
return;
}
const rest = new REST({
version: "10",
userAgentAppendix: "Supportmail (https://supportmail.dev 3.0)",
authPrefix: "Bot",
globalRequestsPerSecond: 5,
rejectOnRateLimit: (data) => {
console.warn("Rate limited by Discord API", inspect(data, { depth: 3 }));
return false;
},
}).setToken(BOT_TOKEN);
const invites: APIInvite[] = [];
for (const { code } of featuredInvites) {
try {
const invite = (await rest.get(`${Routes.invite(code)}?with_counts=true`)) as APIInvite;
invites.push(invite);
if (code !== featuredInvites[featuredInvites.length - 1].code) {
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second after the last request to ensure we don't hit rate limit (we got 6 invites and the limit is 5 req/s)
}
} catch (error) {
console.error(`Failed to fetch invite for code ${code}:`, error);
}
}
console.log(`Fetched ${invites.length} invites`);
// save to file
await options.fs.writeFile(
"src/lib/server/invites/invites.ts",
`export const invites = ${JSON.stringify(invites, null, 2)} as MyInvite[];`,
);
console.log("Invites file generated successfully");
return;
},
};
}