-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
92 lines (79 loc) · 2.5 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
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
89
90
91
92
const fs = require("fs");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const Discord = require("discord.js");
const logger = require("./logger");
dotenv.config();
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildBans,
Discord.GatewayIntentBits.GuildInvites,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildPresences,
],
});
async function init() {
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
// Connect to MongoDB
await mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
logger.info("Connected to MongoDB");
})
.catch((err) => {
logger.error("Error connecting to MongoDB", {
error: err.message || null,
stack: err.stack || null,
});
process.exit(1);
});
// Load commands and events
const commandFolders = fs.readdirSync(__dirname + "/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(__dirname + `/commands/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(__dirname + `/commands/${folder}/${file}`);
client.commands.set(command.name, command);
logger.info(`Loaded command ${command.name}`);
}
}
const eventFiles = fs
.readdirSync(__dirname + "/events")
.filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const event = require(__dirname + `/events/${file}`);
const eventName = file.split(".")[0];
client.events.set(eventName, event);
client.on(eventName, event.bind(null, client));
logger.info(`Loaded event ${eventName}`);
}
client.login(process.env.DISCORD_TOKEN);
}
// Graceful shutdown. Close the database connection before exiting
process.on("SIGTERM", async () => {
logger.info("SIGTERM signal received, shutting down gracefully");
await mongoose.disconnect();
process.exit(0);
});
// Catch any uncaught exceptions or unhandled rejections
process.on("uncaughtException", (error) => {
logger.error("uncaughtException", {
error: error.message || null,
});
});
process.on("unhandledRejection", (error) => {
logger.warn("unhandledRejection:\n", {
error: error?.message || null,
stack: error?.stack || null,
});
});
// Start the bot
init();