-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (147 loc) · 4.99 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Require the necessary discord.js classes
const fs = require("node:fs");
const path = require("node:path");
const {
Client,
Collection,
Events,
GatewayIntentBits,
Partials,
InteractionType,
} = require("discord.js");
const { TOKEN, DB_DATABASE_NAME } = require("./config.json");
const { handleModalInteraction } = require("./events/modalHandler.js");
const { handleButtonInteraction } = require("./events/buttonHandler.js");
const {
handleSelectMenuInteraction,
} = require("./events/selectMenuHandler.js");
const { handleVoiceState } = require("./events/voiceHandler.js");
const { Database } = require("./database");
const {
initReminderHandler,
checkReminders,
} = require("./events/reminderHandler.js");
const { initBirthdayHandler, checkBirthdays } = require("./events/birthdayHandler.js");
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
const db = new Database((originModule = "INDEX"));
const run = async () => {
try {
// Connect the client to the server (optional starting in v4.7)
const dbClient = await db.connect();
// Send a ping to confirm a successful connection
await dbClient.db(`${DB_DATABASE_NAME}`).command({ ping: 1 });
console.log(
"Pinged your deployment. You successfully connected to MongoDB!"
);
} catch (error) {
console.log({ error: error });
}
};
client.commands = new Collection();
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
}
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
initReminderHandler().then(() => {
setInterval(() => checkReminders(client), 60000);
});
initBirthdayHandler(client);
checkBirthdays(client);
});
client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
return await handleVoiceState(oldState, newState);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (
!interaction.isChatInputCommand() &&
!interaction.isMessageContextMenuCommand() &&
!interaction.isButton() &&
!interaction.isModalSubmit
)
return;
if (interaction.type === InteractionType.ModalSubmit) {
return await handleModalInteraction(interaction);
} else if (interaction.isButton()) {
return await handleButtonInteraction(interaction);
} else if (interaction.isStringSelectMenu()) {
return handleSelectMenuInteraction(interaction);
} else if (interaction.isChannelSelectMenu()) {
return handleSelectMenuInteraction(interaction);
} else {
let commandCaller = interaction.commandName;
console.log(`Command called: ${commandCaller}`);
const command = interaction.client.commands.get(commandCaller);
if (!command) {
console.error(`No command matching ${commandCaller} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
ephemeral: true,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
}
});
// Listen for process termination signals
process.on('SIGINT', () => {
console.log('Received SIGINT. Closing database connection...');
shutdown();
});
process.on('SIGTERM', () => {
console.log('Received SIGTERM. Closing database connection...');
shutdown();
});
// Function to gracefully close the database connection
const shutdown = async () => {
try {
await db.disconnect();
console.log('Database connection closed.');
process.exit(0); // Exit with success code
} catch (error) {
console.error('Error closing database connection:', error);
process.exit(1); // Exit with error code
}
};
// Log in to Discord with your client's TOKEN
client.login(TOKEN);
run().catch(console.dir);