import { ShardingManager } from 'discord.js' ; import 'reflect-metadata' ; import { Api } from './api' ; import { GuildsController , RootController , ShardsController } from './controllers' ; import { Job , UpdateServerCountJob } from './jobs' ; import { Manager } from './manager' ; import { HttpService , JobService , Logger , MasterApiService } from './services' ; import { MathUtils , ShardUtils } from './utils' ; let Config = require ( '../config/config.json' ) ; let Debug = require ( '../config/debug.json' ) ; let Logs = require ( '../lang/logs.json' ) ; async function start ( ) { Logger . info ( Logs . info . appStarted ) ; let httpService = new HttpService ( ) ; let masterApiService = new MasterApiService ( httpService ) ; if ( Config . clustering . enabled ) { await masterApiService . register ( ) ; } let shardList ; let totalShards ; try { if ( Config . clustering . enabled ) { let resBody = await masterApiService . login ( ) ; shardList = resBody . shardList ; let requiredShards = await ShardUtils . requiredShardCount ( Config . client . token ) ; totalShards = Math . max ( requiredShards , resBody . totalShards ) ; } else { let recommendedShards = await ShardUtils . recommendedShardCount ( Config . client . token , Config . sharding . serversPerShard ) ; shardList = MathUtils . range ( 0 , recommendedShards ) ; totalShards = recommendedShards ; } } catch ( error ) { Logger . error ( Logs . error . retrieveShards , error ) ; return ; } if ( shardList . length === 0 ) { Logger . warn ( Logs . warn . managerNoShards ) ; return ; } let shardManager = new ShardingManager ( 'dist/start.js' , { token : Config . client . token , mode : Debug . override . shardMode . enabled ? Debug . override . shardMode . value : 'worker' , respawn : true , totalShards , shardList , } ) ; let jobs = [ Config . clustering . enabled ? undefined : new UpdateServerCountJob ( shardManager , httpService ) , ] . filter ( Boolean ) ; let manager = new Manager ( shardManager , new JobService ( jobs ) ) ; let guildsController = new GuildsController ( shardManager ) ; let shardsController = new ShardsController ( shardManager ) ; let rootController = new RootController ( ) ; let api = new Api ( [ guildsController , shardsController , rootController ] ) ; await manager . start ( ) ; await api . start ( ) ; if ( Config . clustering . enabled ) { await masterApiService . ready ( ) ; } } process . on ( 'unhandledRejection' , ( reason , _promise ) => { Logger . error ( Logs . error . unhandledRejection , reason ) ; } ) ; start ( ) . catch ( error => { Logger . error ( Logs . error . unspecified , error ) ; } ) ;