require('dotenv').config(); const isDebug = ( process.argv[2] === 'debug' ); const got = require('got').extend( { throwHttpErrors: false, timeout: 30000, headers: { 'User-Agent': 'Wiki-Bot/' + ( isDebug ? 'testing' : process.env.npm_package_version ) + ' (Discord; ' + process.env.npm_package_name + ')' } } ); const {ShardingManager} = require('discord.js'); const manager = new ShardingManager( './bot.js', { execArgv: ['--icu-data-dir=node_modules/full-icu'], shardArgs: ( isDebug ? ['debug'] : [] ), token: process.env.token } ); var diedShards = 0; manager.on( 'shardCreate', shard => { console.log( `- Shard[${shard.id}]: Launched` ); shard.on( 'spawn', message => { console.log( `- Shard[${shard.id}]: Spawned` ); shard.send( { shard: { id: shard.id } } ); } ); shard.on( 'message', message => { if ( message === 'SIGKILL' ) { console.log( '\n- Killing all shards!\n\n' ); manager.shards.forEach( shard => shard.kill() ); } if ( message === 'postStats' ) postStats(); } ); shard.on( 'death', message => { if ( manager.respawn === false ) diedShards++; if ( ![null, 0].includes( message.exitCode ) ) { if ( !shard.ready ) { manager.respawn = false; console.log( `\n\n- Shard[${shard.id}]: Died due to fatal error, disable respawn!\n\n` ); } else console.log( `\n\n- Shard[${shard.id}]: Died due to fatal error!\n\n` ); } } ); } ); manager.spawn().then( shards => { if ( !isDebug ) { var botList = JSON.parse(process.env.botlist); for ( let [key, value] of Object.entries(botList) ) { if ( !value ) delete botList[key]; } setInterval( postStats, 10800000, botList, shards.size ).unref(); } }, error => { console.error( '- Error while spawning the shards: ' + error ); manager.respawnAll(); } ); /** * Post bot statistics to bot lists. * @param {Object} botList - The list of bot lists to post to. * @param {Number} shardCount - The total number of shards. */ function postStats(botList = JSON.parse(process.env.botlist), shardCount = manager.totalShards) { manager.fetchClientValues('guilds.cache.size').then( results => { var guildCount = results.reduce( (acc, val) => acc + val, 0 ); console.log( '- Current server count: ' + guildCount + '\n' + results.map( (count, i) => { return '-- Shard[' + i + ']: ' + count; } ).join('\n') ); got.post( 'https://botblock.org/api/count', { json: Object.assign( { bot_id: process.env.bot, server_count: guildCount, shard_count: shardCount, shards: results }, botList ), responseType: 'json' } ).then( response => { var body = response.body; if ( response.statusCode !== 200 || !body || body.error ) { console.log( '- ' + response.statusCode + ': Error while posting statistics to BotBlock.org: ' + ( body && body.message ) ); return; } for ( let [key, value] of Object.entries(body.failure) ) { console.log( '- ' + value[0] + ': Error while posting statistics to ' + key + ': ' + value[1] ); } }, error => { console.log( '- Error while posting statistics to BotBlock.org: ' + error ); } ); }, error => console.log( '- Error while getting the guild count: ' + error ) ); } /** * End the process gracefully. * @param {String} signal - The signal received. */ async function graceful(signal) { console.log( '- ' + signal + ': Disabling respawn...' ); manager.respawn = false; } process.once( 'SIGINT', graceful ); process.once( 'SIGTERM', graceful ); process.on( 'exit', code => { if ( diedShards >= manager.totalShards ) process.exit(1); } ); if ( isDebug && process.argv[3]?.startsWith( '--timeout:' ) ) { let timeout = process.argv[3].split(':')[1]; console.log( `\n- Close process in ${timeout} seconds!\n` ); setTimeout( () => { console.log( `\n- Running for ${timeout} seconds, closing process!\n` ); manager.shards.forEach( shard => shard.kill() ); }, timeout * 1000 ).unref(); }