Skip to content

Commit f9fd99b

Browse files
committed
feat: implement database connection and graceful shutdown in bot
1 parent e3ce6a0 commit f9fd99b

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import { Client, GatewayIntentBits } from "discord.js";
2+
import { connectDatabase, disconnectDatabase } from "./database/operations.js";
23
import { config } from "./env.js";
34
import { loadCommands, registerCommands } from "./utils/commands.js";
45
import { loadEvents } from "./utils/events.js";
56

67
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
78

9+
// Connect to database before starting bot
10+
await connectDatabase();
11+
812
loadCommands(client);
913
loadEvents(client);
1014
registerCommands();
1115

16+
// Graceful shutdown
17+
process.on("SIGINT", async () => {
18+
console.log("Shutting down...");
19+
await disconnectDatabase();
20+
process.exit(0);
21+
});
22+
23+
process.on("SIGTERM", async () => {
24+
console.log("Shutting down...");
25+
await disconnectDatabase();
26+
process.exit(0);
27+
});
28+
1229
client.login(config.discord.token);

0 commit comments

Comments
 (0)