1
0

main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. require('dotenv').config();
  2. const isDebug = ( process.argv[2] === 'debug' );
  3. const got = require('got').extend( {
  4. throwHttpErrors: true,
  5. timeout: 5000,
  6. headers: {
  7. 'User-Agent': 'Wiki-Bot/' + ( isDebug ? 'testing' : process.env.npm_package_version ) + ' (Discord; ' + process.env.npm_package_name + ')'
  8. }
  9. } );
  10. const {ShardingManager} = require('discord.js');
  11. const manager = new ShardingManager( './bot.js', {
  12. execArgv: ['--icu-data-dir=node_modules/full-icu'],
  13. shardArgs: ( isDebug ? ['debug'] : [] ),
  14. token: process.env.token
  15. } );
  16. var diedShards = 0;
  17. manager.on( 'shardCreate', shard => {
  18. console.log( `\n- Shard[${shard.id}]: Launched` );
  19. shard.on( 'spawn', message => {
  20. console.log( `- Shard[${shard.id}]: Spawned` );
  21. shard.send( {
  22. shard: {
  23. id: shard.id
  24. }
  25. } );
  26. } );
  27. shard.on( 'message', message => {
  28. if ( message === 'SIGKILL' ) {
  29. console.log( '\n- Killing all shards!\n\n' );
  30. manager.shards.forEach( shard => shard.kill() );
  31. }
  32. if ( message === 'postStats' ) postStats();
  33. } );
  34. shard.on( 'death', message => {
  35. if ( manager.respawn === false ) diedShards++;
  36. if ( message.exitCode !== 0 ) {
  37. if ( !shard.ready ) {
  38. manager.respawn = false;
  39. console.log( `\n\n- Shard[${shard.id}]: Died due to fatal error, disable respawn!\n\n` );
  40. }
  41. else console.log( `\n\n- Shard[${shard.id}]: Died due to fatal error!\n\n` );
  42. }
  43. } );
  44. } );
  45. manager.spawn().then( shards => {
  46. if ( !isDebug ) setInterval( postStats, 10800000, shards.size ).unref();
  47. }, error => {
  48. console.error( '- Error while spawning the shards: ' + error );
  49. manager.respawnAll();
  50. } );
  51. function postStats(shardCount = manager.totalShards) {
  52. manager.fetchClientValues('guilds.cache.size').then( results => {
  53. var guildCount = results.reduce( (acc, val) => acc + val, 0 );
  54. console.log( '- Current server count: ' + guildCount + '\n' + results.map( (count, i) => {
  55. return '-- Shard[' + i + ']: ' + count;
  56. } ).join('\n') );
  57. if ( process.env.toptoken ) got.post( 'https://top.gg/api/bots/' + process.env.bot + '/stats', {
  58. headers: {
  59. Authorization: process.env.toptoken
  60. },
  61. json: {
  62. server_count: guildCount,
  63. shards: results,
  64. shard_count: shardCount
  65. },
  66. responseType: 'json'
  67. } ).catch( error => {
  68. console.log( '- Error while posting statistics to https://top.gg/bot/' + process.env.bot + ': ' + error );
  69. } );
  70. if ( process.env.dbggtoken ) got.post( 'https://discord.bots.gg/api/v1/bots/' + process.env.bot + '/stats', {
  71. headers: {
  72. Authorization: process.env.dbggtoken
  73. },
  74. json: {
  75. guildCount: guildCount,
  76. shardCount: shardCount
  77. },
  78. responseType: 'json'
  79. } ).catch( error => {
  80. console.log( '- Error while posting statistics to https://discord.bots.gg/bots/' + process.env.bot + ': ' + error );
  81. } );
  82. if ( process.env.bodtoken ) got.post( 'https://bots.ondiscord.xyz/bot-api/bots/' + process.env.bot + '/guilds', {
  83. headers: {
  84. Authorization: process.env.bodtoken
  85. },
  86. json: {
  87. guildCount: guildCount
  88. },
  89. responseType: 'json'
  90. } ).catch( error => {
  91. console.log( '- Error while posting statistics to https://bots.ondiscord.xyz/bots/' + process.env.bot + ': ' + error );
  92. } );
  93. if ( process.env.dbltoken ) got.post( 'https://discordbotlist.com/api/v1/bots/' + process.env.bot + '/stats', {
  94. headers: {
  95. Authorization: process.env.dbltoken
  96. },
  97. json: {
  98. guilds: guildCount
  99. },
  100. responseType: 'json'
  101. } ).catch( error => {
  102. console.log( '- Error while posting statistics to https://discordbotlist.com/bots/' + process.env.bot + ': ' + error );
  103. } );
  104. }, error => console.log( '- Error while getting the guild count: ' + error ) );
  105. }
  106. async function graceful(signal) {
  107. console.log( '- ' + signal + ': Disabling respawn...' );
  108. manager.respawn = false;
  109. }
  110. process.once( 'SIGINT', graceful );
  111. process.once( 'SIGTERM', graceful );
  112. process.on( 'exit', code => {
  113. if ( diedShards >= manager.totalShards ) process.exit(1);
  114. } );