-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·57 lines (46 loc) · 1.61 KB
/
index.js
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
import { Client, IntentsBitField, Partials } from "discord.js";
import OpenAI from "openai";
import "dotenv/config.js";
import interactionCreate from "./events/interactionCreate.js";
import messageCreate from "./events/messageCreate.js";
import ready from "./events/ready.js";
import createDallEImage from "./utils/createDallEImage.js";
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
IntentsBitField.Flags.DirectMessages,
IntentsBitField.Flags.DirectMessageReactions,
IntentsBitField.Flags.DirectMessageTyping,
],
partials: [Partials.Channel, Partials.Reaction, Partials.Message],
debug: true,
});
client.openai = new OpenAI({
apiKey: process.env.API_KEY,
});
client.createDallEImage = (description) =>
createDallEImage(client, description);
client.on("ready", () => ready(client));
client.on("messageCreate", (message) => messageCreate(message, client));
client.on("interactionCreate", (interaction) =>
interactionCreate(interaction, client)
);
client.login(process.env.TOKEN);
let isShuttingDown = false;
async function gracefulShutdown() {
if (isShuttingDown) return; // Prevent multiple shutdowns
isShuttingDown = true;
console.log("Shutting down gracefully...");
// Shut down the Discord client
try {
await client.destroy();
console.log("Bot logged out from Discord");
} catch (error) {
console.error("Error during Discord client shutdown:", error);
}
}
// Signal handling for graceful shutdown
process.on("SIGINT", gracefulShutdown);
process.on("SIGTERM", gracefulShutdown);