-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
88 lines (70 loc) · 2.3 KB
/
index.ts
File metadata and controls
88 lines (70 loc) · 2.3 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
82
83
84
85
86
87
88
// Check if all the required environment variables are set
import fs from "fs/promises";
import path from "path";
import Bun from "bun";
import {
Client,
GatewayIntentBits,
REST,
Routes,
type APIApplicationCommand,
} from "discord.js";
import { env } from "./config.ts";
import commandsMap from "./commands.ts";
import { getTwitchToken } from "./utils/twitch/auth.ts";
import updateGuildsOnStartup from "./utils/discord/updateGuildsOnStartup.ts";
if (!env.discordToken || env.discordToken === "YOUR_DISCORD_TOKEN") {
throw new Error("You MUST provide a discord token in .env!");
}
if (!env.youtubeApiKey || env.youtubeApiKey === "YOUR_YOUTUBE_API_KEY") {
throw new Error("You MUST provide a YouTube API key in .env!");
}
if (!env.twitchClientId || env.twitchClientId === "YOUR_TWITCH_CLIENT_ID") {
throw new Error("You MUST provide a Twitch client ID in .env!");
}
if (
!env.twitchClientSecret ||
env.twitchClientSecret === "YOUR_TWITCH_CLIENT_SECRET"
) {
throw new Error("You MUST provide a Twitch client secret in .env!");
}
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});
// Update the commands
console.log(`Refreshing ${commandsMap.size} commands`);
const rest = new REST().setToken(env.discordToken);
const getAppId: { id?: string | null } = (await rest.get(
Routes.currentApplication(),
)) || { id: null };
if (!getAppId?.id)
throw "No application ID was able to be found with this token";
const data = (await rest.put(Routes.applicationCommands(getAppId.id), {
body: [...commandsMap.values()].map((a) => {
return a.data;
}),
})) as APIApplicationCommand[];
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// Get Twitch token
if (!(await getTwitchToken())) {
throw new Error("Error getting Twitch token");
}
// Login to Discord
client.login(env.discordToken);
export default client;
// Import events
const getEvents = await fs.readdir(path.resolve(__dirname, "./events"));
await Promise.all(
getEvents.map(async (file) => {
await import("./events/" + file);
}),
);
// Update the guilds on startup
await updateGuildsOnStartup();
// Attempt the garbage collection every hour
setInterval(
() => {
Bun.gc(true);
},
60 * 60 * 1000,
);