-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
344 lines (293 loc) · 11 KB
/
index.js
File metadata and controls
344 lines (293 loc) · 11 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const fs = require('fs');
const path = require('path');
const { Client, Collection, GatewayIntentBits, REST, Routes, ActivityType, MessageFlags } = require('discord.js');
const mongoose = require('mongoose');
// ======================
// Load settings from config.json
// ======================
let config;
try {
const configPath = path.join(__dirname, 'config.json');
if (!fs.existsSync(configPath)) {
throw new Error('❌ config.json file not found. Please create the file based on config.example.json');
}
const configFile = fs.readFileSync(configPath, 'utf8');
config = JSON.parse(configFile);
// Validate required values
if (!config.DISCORD_TOKEN) {
throw new Error('❌ DISCORD_TOKEN not found in config.json');
}
if (!config.CLIENT_ID) {
throw new Error('❌ CLIENT_ID not found in config.json');
}
console.log('✅ Successfully loaded settings from config.json');
} catch (error) {
console.error('❌ Failed to load config.json:');
console.error(error.message);
process.exit(1);
}
// ======================
// Unhandled error handling
// ======================
process.on('unhandledRejection', error => {
console.error('⚠️ Unhandled promise rejection:', error);
});
process.on('uncaughtException', error => {
console.error('⚠️ Uncaught exception:', error);
});
process.on('uncaughtExceptionMonitor', error => {
console.error('⚠️ Uncaught exception monitor:', error);
});
// ======================
// Client setup
// ======================
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
]
});
// ======================
// Storage collections
// ======================
client.commands = new Collection();
client.systems = new Collection();
client.config = config; // Make settings available through client
client.db = null; // Will be set when MongoDB connects
// ======================
// Database connection functions
// ======================
const connectToDatabase = async () => {
if (!config.MONGODB_URI) {
console.warn('⚠️ MONGODB_URI not found in config.json - Skipping database connection');
return null;
}
try {
console.log('🔄 Attempting to connect to database...');
// Recommended connection settings
const connectionOptions = {
maxPoolSize: 10,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
family: 4
};
await mongoose.connect(config.MONGODB_URI, connectionOptions);
console.log('✅ Successfully connected to database');
// Set up connection event listeners
mongoose.connection.on('error', (err) => {
console.error('❌ MongoDB connection error:', err);
});
mongoose.connection.on('disconnected', () => {
console.warn('⚠️ Database connection disconnected');
});
mongoose.connection.on('reconnected', () => {
console.log('✅ Database connection reestablished');
});
return mongoose.connection;
} catch (error) {
console.error('❌ Failed to connect to database:');
console.error('Error details:', error.message);
if (error.name === 'MongoServerSelectionError') {
console.error('🔍 Check:');
console.error(' - MONGODB_URI connection string validity');
console.error(' - Internet connection');
console.error(' - Network settings and firewall');
console.error(' - Credentials validity');
}
return null;
}
};
// ======================
// Helper functions
// ======================
const loadFiles = async (directory, callback) => {
const dirPath = path.join(__dirname, directory);
console.log(`🔍 Searching for files in: ${dirPath}`);
if (!fs.existsSync(dirPath)) {
console.warn(`⚠️ Directory not found: ${directory}`);
return;
}
try {
const files = fs.readdirSync(dirPath).filter(file => file.endsWith('.js'));
console.log(`📂 Found ${files.length} files in ${directory}:`, files);
for (const file of files) {
const filePath = path.join(dirPath, file);
try {
// Clear cache to ensure loading latest version
delete require.cache[require.resolve(filePath)];
const loadedFile = require(filePath);
await callback(file, loadedFile);
console.log(`✅ Successfully loaded: ${file}`);
} catch (err) {
console.error(`❌ Failed to load file: ${filePath}`);
console.error(err.stack || err);
// If error is related to config loading, handle differently
if (err.message.includes('config.json')) {
console.error(`🔧 Issue in: ${file} - Trying to load config.json itself`);
}
}
}
} catch (err) {
console.error(`❌ Error reading directory: ${dirPath}`);
console.error(err.stack || err);
}
};
// ======================
// Load commands
// ======================
const loadSlashCommands = async () => {
const commands = [];
await loadFiles('commands', (file, command) => {
if (!command.data || !command.execute) {
console.warn(`⚠️ Skipping ${file}: Missing data or execute function`);
return;
}
try {
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
console.log(`🔄 Loaded command: ${command.data.name}`);
} catch (err) {
console.error(`❌ Error processing command ${file}:`);
console.error(err.stack || err);
}
});
if (commands.length === 0) {
console.warn('⚠️ No commands were loaded!');
return;
}
try {
const rest = new REST({ version: '10' }).setToken(config.DISCORD_TOKEN);
console.log('🔄 Refreshing application (/) commands...');
const data = await rest.put(
Routes.applicationCommands(config.CLIENT_ID),
{ body: commands }
);
console.log(`✅ Successfully reloaded ${data.length} application commands.`);
} catch (err) {
console.error('❌ Failed to reload commands:');
console.error(err.stack || err);
}
};
// ======================
// Load systems
// ======================
const loadSystems = async () => {
await loadFiles('systems', (file, system) => {
if (!system.name || typeof system.execute !== 'function') {
console.warn(`⚠️ Skipping ${file}: Missing name or execute function`);
return;
}
try {
// Pass client only - Systems should use client.config
system.execute(client);
client.systems.set(system.name, system);
console.log(`🔄 System initialized: ${system.name}`);
} catch (err) {
console.error(`❌ Error initializing system ${system.name}:`);
console.error(err.stack || err);
}
});
};
// ======================
// Event handling
// ======================
client.on('interactionCreate', async (interaction) => {
// Handle autocomplete interactions
if (interaction.isAutocomplete()) {
const command = client.commands.get(interaction.commandName);
if (!command || !command.autocomplete) return;
try {
await command.autocomplete(interaction);
} catch (error) {
// 10062 = interaction token expired (user typed faster than round-trip) — not a real error
if (error.code !== 10062) {
console.error(`❌ Error in autocomplete for ${interaction.commandName}:`, error);
}
}
return;
}
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) {
console.error(`❌ Command not found: ${interaction.commandName}`);
return interaction.reply({
content: '❌ This command is currently unavailable.',
ephemeral: true
});
}
try {
console.log(`🔧 Executing command: ${interaction.commandName}`);
await command.execute(client, interaction);
} catch (error) {
console.error(`❌ Error executing ${interaction.commandName}:`);
console.error(error.stack || error);
if (interaction.replied || interaction.deferred) {
try {
await interaction.followUp({
content: '❌ An error occurred while executing the command.',
flags: MessageFlags.Ephemeral
});
} catch { /* interaction may have expired */ }
} else {
try {
await interaction.reply({
content: '❌ An error occurred while executing the command.',
flags: MessageFlags.Ephemeral
});
} catch { /* interaction may have expired */ }
}
}
});
// ======================
// Graceful application shutdown
// ======================
const gracefulShutdown = async () => {
console.log('🔄 Shutting down application gracefully...');
try {
// Close Discord connection
if (client && !client.destroyed) {
client.destroy();
console.log('✅ Discord connection closed');
}
// Close MongoDB connection
if (mongoose.connection.readyState !== 0) {
await mongoose.connection.close();
console.log('✅ Database connection closed');
}
console.log('👋 Application closed successfully');
process.exit(0);
} catch (error) {
console.error('❌ Error during shutdown:', error);
process.exit(1);
}
};
// Handle shutdown signals
process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
// ======================
// Startup
// ======================
const startBot = async () => {
try {
// Connect to database first
const dbConnection = await connectToDatabase();
client.db = dbConnection;
// Then load systems and commands
await loadSystems();
await loadSlashCommands();
// Finally start the bot
await client.login(config.DISCORD_TOKEN);
// Set bot activity after ready event
client.once('ready', () => {
console.log(`🤖 Bot is now online as: ${client.user.tag}`);
client.user.setActivity('Subscriptions', { type: ActivityType.Watching });
});
} catch (err) {
console.error('❌ Failed to start bot:');
console.error(err.stack || err);
await gracefulShutdown();
}
};
startBot();