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 ); 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(); client.modals = new Collection(); /* * START APPLICATION */ async function start() { try { /* * Connect Mongo first. */ await mClient.connect(); console.log( "MongoDB connected" ); /* * Initialize database indexes before handlers are * exposed to Discord interactions. * * Requiring these after mClient is exported avoids * circular-import initialization problems because the * shared utilities import mClient from index.js. */ const { ensureCookieIndexes } = require( "./utils/cookieEconomy" ); const { ensureProfileSystem } = require( "./utils/profileSystem" ); await Promise.all([ ensureCookieIndexes(), ensureProfileSystem() ]); console.log( "Database indexes initialized" ); /* * Load handlers. */ const handlerFiles = fs.readdirSync( "./handlers" ); for ( const handler of handlerFiles ) { require( `./handlers/${handler}` )(client); } /* * Login Discord. */ await client.login( process.env.D_TOKEN ); } catch (error) { console.error( "Failed to start ShaiBot:", error ); process.exit(1); } } start();