Added New Db Stuff
This commit is contained in:
61
index.js
61
index.js
@ -2,7 +2,8 @@ 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 } = require('./db')
|
||||
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 ] });
|
||||
|
||||
@ -54,20 +55,54 @@ function loadEvents(dir, client) {
|
||||
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);
|
||||
});
|
||||
|
||||
connectDB().then(() => {
|
||||
client.login(process.env.TOKEN).catch(console.error)
|
||||
// Optional: observe REST & rate limits
|
||||
client.rest.on('rateLimited', (info) => {
|
||||
console.warn('[REST rateLimit]', info);
|
||||
});
|
||||
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Bot is shutting down...')
|
||||
closeDB();
|
||||
process.exit(0)
|
||||
});
|
||||
// ---- 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);
|
||||
}
|
||||
})();
|
||||
|
||||
process.on('SIGTERM', async () => {
|
||||
console.log('Bot received termination signal...');
|
||||
closeDB();
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
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'));
|
||||
Reference in New Issue
Block a user