const { Client, GatewayIntentBits, Collection, Partials } = require("discord.js"); const { MongoClient } = require("mongodb"); const fs = require("node:fs"); require("dotenv").config(); /* * ============================================================ * MONGODB * ============================================================ */ const mClient = new MongoClient( process.env.M_URI ); /* * Export this BEFORE handlers/commands are loaded. * * utils/nutEconomy.js imports mClient from index.js. */ exports.mClient = mClient; /* * ============================================================ * DISCORD CLIENT * ============================================================ */ const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent ], partials: [ Partials.Channel, Partials.Message, Partials.User, Partials.GuildMember, Partials.Reaction ] }); exports.client = client; /* * ============================================================ * COLLECTIONS * ============================================================ */ client.commands = new Collection(); client.legacyCommands = new Collection(); client.aliases = new Collection(); client.buttons = new Collection(); client.selectMenus = new Collection(); /* * ============================================================ * STARTUP * ============================================================ */ async function start() { try { /* * Connect Mongo first. */ await mClient.connect(); console.log( "[MongoDB] Connected" ); /* * Load handlers only after MongoDB is ready. */ const handlers = fs.readdirSync( "./handlers" ); for ( const handler of handlers ) { require( `./handlers/${handler}` )( client ); } /* * Finally connect to Discord. */ await client.login( process.env.D_Token ); } catch (error) { console.error( "Failed to start bot:", error ); process.exit( 1 ); } } /* * ============================================================ * SHUTDOWN * ============================================================ */ async function shutdown( signal ) { console.log( `[System] ${signal} received, shutting down...` ); try { client.destroy(); } catch {} try { await mClient.close(); } catch {} process.exit( 0 ); } process.on( "SIGINT", () => shutdown( "SIGINT" ) ); process.on( "SIGTERM", () => shutdown( "SIGTERM" ) ); /* * ============================================================ * START * ============================================================ */ start();