-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
80 lines (69 loc) · 1.88 KB
/
client.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
import { Octokit } from "@octokit/rest";
import { serve } from "bun";
import { Client, GatewayIntentBits, Partials } from "discord.js";
import interactionCreateHandler from "./events/interactions";
import messageCreateHandler from "./events/messages";
import logger from "./logger";
const requiredEnvVars = [
"BOT_TOKEN",
"GITHUB_TOKEN",
"GUILD_ID_TEST",
"GUILD_ID_PROD",
"CLIENT_ID",
"GITHUB_ORG",
];
for (const varName of requiredEnvVars) {
if (!process.env[varName]) {
logger.error(`Missing required environment variable: ${varName}`);
process.exit(1);
}
}
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
partials: [Partials.Channel],
});
// Event: Ready
client.once("ready", () => {
if (!client.user) {
logger.error("Client user is not available.");
return;
}
logger.info(`${client.user.tag} is ready to serve!`);
});
// Handle interactions (slash commands)
interactionCreateHandler(client, octokit);
// Handle message-based commands
client.on("messageCreate", async (message) => {
await messageCreateHandler(message, client, octokit);
});
// Graceful shutdown
process.on("SIGINT", () => {
logger.info("Shutting down gracefully...");
client.destroy();
process.exit();
});
process.on("SIGTERM", () => {
logger.info("Shutting down gracefully...");
client.destroy();
process.exit();
});
client.login(process.env.BOT_TOKEN);
serve({
fetch: (req) => {
const url = new URL(req.url);
if (url.pathname === "/api/healthz") {
return new Response("OK", { status: 200 });
}
return new Response("Not Found", { status: 404 });
},
port: process.env.PORT || 3001,
});
console.log(`Server running on port ${process.env.PORT || 3001}`);