-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
59 lines (52 loc) · 1.75 KB
/
index.ts
File metadata and controls
59 lines (52 loc) · 1.75 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
// index.ts
import { Client, GatewayIntentBits, Partials } from "discord.js";
import OpenAI from "openai";
import "dotenv/config";
import { DEFAULT_MODEL } from "./config.js";
import interactionCreate from "./events/interactionCreate.js";
import messageCreate from "./events/messageCreate.js";
import ready from "./events/ready.js";
import { initializeMedicinePosts } from "./features/medicinePosts.js";
import { initializeEponymPosts } from "./features/eponymPosts.js";
import type { ExtendedClient } from "./types/ExtendedClient.js";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
],
partials: [Partials.Channel, Partials.Reaction, Partials.Message],
}) as ExtendedClient;
client.openai = new OpenAI({
apiKey: process.env.API_KEY,
});
client.currentModel = DEFAULT_MODEL;
client.on("ready", () => {
ready(client);
initializeMedicinePosts(client);
initializeEponymPosts(client);
});
client.on("messageCreate", (message) => messageCreate(message, client));
client.on("interactionCreate", (interaction) =>
interactionCreate(interaction, client)
);
client.login(process.env.TOKEN);
// Graceful shutdown
let isShuttingDown = false;
async function gracefulShutdown(): Promise<void> {
if (isShuttingDown) return;
isShuttingDown = true;
console.log("Shutting down gracefully...");
try {
await client.destroy();
console.log("Bot logged out from Discord");
} catch (error) {
console.error("Error during shutdown:", error);
}
process.exit(0);
}
process.on("SIGINT", gracefulShutdown);
process.on("SIGTERM", gracefulShutdown);