108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
const fs = require('node:fs');
|
|
const path = require('node:path')
|
|
const {Client, Events, GatewayIntentBits, Collection, InteractionResponse } = require('discord.js');
|
|
require('dotenv').config();
|
|
const { connectDB, closeDB, pool } = require('./db')
|
|
const { initStatusCache } = require('./events/ready/db-status');
|
|
|
|
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildModeration, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildPresences ] });
|
|
|
|
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.`);
|
|
}
|
|
}
|
|
}
|
|
|
|
function loadEvents(dir, client) {
|
|
const files = fs.readdirSync(dir, { withFileTypes: true });
|
|
|
|
for (const file of files) {
|
|
const fullPath = path.join(dir, file.name);
|
|
|
|
if (file.isDirectory()) {
|
|
// Recursively load events from subdirectories
|
|
loadEvents(fullPath, client);
|
|
} else if (file.isFile() && file.name.endsWith('.js')) {
|
|
// Load the event file
|
|
const event = require(fullPath);
|
|
|
|
if (event.once) {
|
|
client.once(event.name, (...args) => event.execute(...args));
|
|
} else {
|
|
client.on(event.name, (...args) => event.execute(...args));
|
|
}
|
|
|
|
//console.log(`Loaded event: ${event.name}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Call the function to load all events
|
|
const eventsPath = path.join(__dirname, 'events');
|
|
loadEvents(eventsPath, client);
|
|
|
|
// ---- Global guards (super helpful in prod)
|
|
process.on('unhandledRejection', (err) => {
|
|
console.error('UNHANDLED REJECTION:', err);
|
|
});
|
|
process.on('uncaughtException', (err) => {
|
|
console.error('UNCAUGHT EXCEPTION:', err);
|
|
});
|
|
|
|
// Optional: observe REST & rate limits
|
|
client.rest.on('rateLimited', (info) => {
|
|
console.warn('[REST rateLimit]', info);
|
|
});
|
|
|
|
|
|
// ---- Boot sequence
|
|
(async () => {
|
|
try {
|
|
await connectDB(); // verify DB first
|
|
await initStatusCache(pool);
|
|
await client.login(process.env.TOKEN);
|
|
//console.log(`✅ Logged in as ${client.user?.tag}`);
|
|
} catch (err) {
|
|
console.error('Startup error:', err);
|
|
// ensure we close DB if login fails
|
|
try { await closeDB(); } catch {}
|
|
process.exit(1);
|
|
}
|
|
})();
|
|
|
|
|
|
async function shutdown(signal) {
|
|
console.log(`${signal} received, shutting down…`);
|
|
try {
|
|
if (client.isReady()) {
|
|
await client.destroy(); // closes WS cleanly
|
|
}
|
|
} catch (e) {
|
|
console.error('Error during client.destroy():', e);
|
|
}
|
|
|
|
try {
|
|
await closeDB(); // pool.end()
|
|
} catch (e) {
|
|
console.error('Error during DB close:', e);
|
|
}
|
|
|
|
process.exit(0);
|
|
}
|
|
|
|
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
process.on('SIGTERM', () => shutdown('SIGTERM')); |