@@ -20,15 +20,14 @@ class TitanBot extends Client {
2020
2121 GatewayIntentBits . Guilds ,
2222 GatewayIntentBits . GuildMembers ,
23-
24-
23+
2524 GatewayIntentBits . GuildMessages ,
2625 GatewayIntentBits . GuildMessageReactions ,
2726 GatewayIntentBits . MessageContent ,
28-
27+ GatewayIntentBits . DirectMessages ,
28+
2929 GatewayIntentBits . GuildVoiceStates ,
30-
31-
30+
3231 GatewayIntentBits . GuildBans ,
3332 ] ,
3433 } ) ;
@@ -52,7 +51,7 @@ class TitanBot extends Client {
5251 startupLog ( 'Initializing database...' ) ;
5352 const dbInstance = await initializeDatabase ( ) ;
5453 this . db = dbInstance . db ;
55-
54+
5655 // Check database status and report
5756 const dbStatus = this . db . getStatus ( ) ;
5857 if ( dbStatus . isDegraded ) {
@@ -196,8 +195,8 @@ class TitanBot extends Client {
196195 hasStartedListening = true ;
197196 this . webServer = server ;
198197 startupLog ( `✅ Web Server running on ${ host } :${ port } ` ) ;
199- startupLog ( `Health endpoint: http://localhost :${ port } /health` ) ;
200- startupLog ( `Ready endpoint: http://localhost :${ port } /ready` ) ;
198+ startupLog ( `Health endpoint: http://${ host } :${ port } /health` ) ;
199+ startupLog ( `Ready endpoint: http://${ host } :${ port } /ready` ) ;
201200 } ) ;
202201
203202 server . on ( 'error' , ( error ) => {
@@ -258,6 +257,7 @@ class TitanBot extends Client {
258257 }
259258 }
260259
260+ // Save cleaned counters if any were orphaned
261261 // Save cleaned counters if any were orphaned
262262 if ( orphanedCounters . length > 0 ) {
263263 await saveServerCounters ( this , guildId , validCounters ) ;
@@ -270,21 +270,23 @@ class TitanBot extends Client {
270270 }
271271
272272 async loadHandlers ( ) {
273+ startupLog ( 'Loading handlers...' ) ;
273274 const handlers = [
274275 { path : 'events' , type : 'default' , required : true } ,
275276 { path : 'interactions' , type : 'default' , required : true }
276277 ] ;
277278
278279 for ( const handler of handlers ) {
279280 try {
281+ startupLog ( `Loading handler: ${ handler . path } ` ) ;
280282 const module = await import ( `./handlers/${ handler . path } .js` ) ;
281- const loaderFn = handler . type . startsWith ( 'named:' )
282- ? module [ handler . type . split ( ':' ) [ 1 ] ]
283+ const loaderFn = handler . type . startsWith ( 'named:' )
284+ ? module [ handler . type . split ( ':' ) [ 1 ] ]
283285 : module . default ;
284-
286+
285287 if ( typeof loaderFn === 'function' ) {
286288 await loaderFn ( this ) ;
287- logger . info ( `✅ Loaded ${ handler . path } ` ) ;
289+ startupLog ( `✅ Loaded ${ handler . path } ` ) ;
288290 } else {
289291 throw new Error ( `Invalid loader export from ${ handler . path } ` ) ;
290292 }
@@ -319,6 +321,7 @@ class TitanBot extends Client {
319321 cron . getTasks ( ) . forEach ( task => task . stop ( ) ) ;
320322 logger . info ( '✅ Cron jobs stopped' ) ;
321323
324+ // Close database connection
322325 // Close database connection
323326 if ( this . db && this . db . db ) {
324327 logger . info ( 'Closing database connection...' ) ;
@@ -332,15 +335,13 @@ class TitanBot extends Client {
332335 }
333336 }
334337
335-
336338 logger . info ( 'Destroying Discord client...' ) ;
337339 if ( this . isReady ( ) ) {
338340 try {
339341 this . destroy ( ) ;
340342 logger . info ( '✅ Discord client destroyed' ) ;
341343 } catch ( error ) {
342-
343-
344+
344345 logger . warn ( 'Discord client destroy warning (non-critical):' , error . message ) ;
345346 }
346347 }
@@ -368,19 +369,25 @@ try {
368369 } ) ;
369370
370371 process . on ( 'unhandledRejection' , ( reason , promise ) => {
372+ const code = reason ?. code ;
373+ if ( code === 10062 || code === 40060 || code === 50027 ) {
374+ logger . warn ( 'Recoverable Discord interaction rejection:' , reason ?. message || reason ) ;
375+ return ;
376+ }
377+
371378 logger . error ( 'Unhandled Rejection at:' , promise , 'reason:' , reason ) ;
372379 bot . shutdown ( 'UNHANDLED_REJECTION' ) ;
373380 } ) ;
374381 } ;
375382
376383 setupShutdown ( ) ;
377- bot . start ( ) ;
384+ bot . start ( ) . catch ( ( error ) => {
385+ logger . error ( 'Fatal error during bot startup:' , error ) ;
386+ bot . shutdown ( 'STARTUP_ERROR' ) ;
387+ } ) ;
378388} catch ( error ) {
379389 logger . error ( 'Fatal error during bot startup:' , error ) ;
380390 process . exit ( 1 ) ;
381391}
382392
383- export default TitanBot ;
384-
385-
386-
393+ export default TitanBot ;
0 commit comments