Browse Source

update botlist stats

Markus-Rost 5 years ago
parent
commit
2d7e7e0ffa
2 changed files with 72 additions and 33 deletions
  1. 0 30
      bot.js
  2. 72 3
      main.js

+ 0 - 30
bot.js

@@ -62,36 +62,6 @@ client.on( 'ready', () => {
 	Object.keys(voice).forEach( guild => {
 	Object.keys(voice).forEach( guild => {
 		if ( !client.guilds.cache.has(guild) ) delete voice[guild];
 		if ( !client.guilds.cache.has(guild) ) delete voice[guild];
 	} );
 	} );
-	
-	if ( !isDebug ) client.setInterval( () => {
-		console.log( '- ' + shardId + ': Current server count: ' + client.guilds.cache.size );
-		if ( process.env.dbltoken ) got.post( 'https://top.gg/api/bots/' + client.user.id + '/stats', {
-			headers: {
-				Authorization: process.env.dbltoken
-			},
-			json: {
-				server_count: client.guilds.cache.size,
-				shard_count: client.shard.count,
-				shard_id: shardId
-			},
-			responseType: 'json'
-		} ).catch( error => {
-			console.log( '- ' + shardId + ': Error while posting statistics to https://top.gg/bot/' + client.user.id + ': ' + error );
-		} );
-		if ( process.env.dbggtoken ) got.post( 'https://discord.bots.gg/api/v1/bots/' + client.user.id + '/stats', {
-			headers: {
-				Authorization: process.env.dbggtoken
-			},
-			json: {
-				guildCount: client.guilds.cache.size,
-				shardCount: client.shard.count,
-				shardId: shardId
-			},
-			responseType: 'json'
-		} ).catch( error => {
-			console.log( '- ' + shardId + ': Error while posting statistics to https://discord.bots.gg/bots/' + client.user.id + ': ' + error );
-		} );
-	}, 10800000 ).unref();
 } );
 } );
 client.on( 'shardDisconnect', () => client.ready = false );
 client.on( 'shardDisconnect', () => client.ready = false );
 
 

+ 72 - 3
main.js

@@ -1,8 +1,17 @@
 require('dotenv').config();
 require('dotenv').config();
+
+const isDebug = ( process.argv[2] === 'debug' );
+const got = require('got').extend( {
+	throwHttpErrors: true,
+	timeout: 5000,
+	headers: {
+		'User-Agent': 'Wiki-Bot/' + ( isDebug ? 'testing' : process.env.npm_package_version ) + ' (Discord; ' + process.env.npm_package_name + ')'
+	}
+} );
 const {ShardingManager} = require('discord.js');
 const {ShardingManager} = require('discord.js');
 const manager = new ShardingManager( './bot.js', {
 const manager = new ShardingManager( './bot.js', {
 	execArgv: ['--icu-data-dir=node_modules/full-icu'],
 	execArgv: ['--icu-data-dir=node_modules/full-icu'],
-	shardArgs: ( process.argv[2] === 'debug' ? ['debug'] : [] ),
+	shardArgs: ( isDebug ? ['debug'] : [] ),
 	token: process.env.token
 	token: process.env.token
 } );
 } );
 
 
@@ -24,6 +33,7 @@ manager.on( 'shardCreate', shard => {
 			console.log( '\n- Killing all shards!\n\n' );
 			console.log( '\n- Killing all shards!\n\n' );
 			manager.shards.forEach( shard => shard.kill() );
 			manager.shards.forEach( shard => shard.kill() );
 		}
 		}
+		if ( message === 'postStats' ) postStats();
 	} );
 	} );
 	
 	
 	shard.on( 'death', message => {
 	shard.on( 'death', message => {
@@ -38,11 +48,70 @@ manager.on( 'shardCreate', shard => {
 	} );
 	} );
 } );
 } );
 
 
-manager.spawn().catch( error => {
-	console.error( '- ' + error.name + ': ' + error.message );
+manager.spawn().then( shards => {
+	if ( !isDebug ) setInterval( postStats, 10800000, shards.size ).unref();
+}, error => {
+	console.error( '- Error while spawning the shards: ' + error );
 	manager.respawnAll();
 	manager.respawnAll();
 } );
 } );
 
 
+function postStats(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') );
+		if ( process.env.toptoken ) got.post( 'https://top.gg/api/bots/' + process.env.bot + '/stats', {
+			headers: {
+				Authorization: process.env.toptoken
+			},
+			json: {
+				server_count: guildCount,
+				shards: results,
+				shard_count: shardCount
+			},
+			responseType: 'json'
+		} ).catch( error => {
+			console.log( '- Error while posting statistics to https://top.gg/bot/' + process.env.bot + ': ' + error );
+		} );
+		if ( process.env.dbggtoken ) got.post( 'https://discord.bots.gg/api/v1/bots/' + process.env.bot + '/stats', {
+			headers: {
+				Authorization: process.env.dbggtoken
+			},
+			json: {
+				guildCount: guildCount,
+				shardCount: shardCount
+			},
+			responseType: 'json'
+		} ).catch( error => {
+			console.log( '- Error while posting statistics to https://discord.bots.gg/bots/' + process.env.bot + ': ' + error );
+		} );
+		if ( process.env.bodtoken ) got.post( 'https://bots.ondiscord.xyz/bot-api/bots/' + process.env.bot + '/guilds', {
+			headers: {
+				Authorization: process.env.bodtoken
+			},
+			json: {
+				guildCount: guildCount
+			},
+			responseType: 'json'
+		} ).catch( error => {
+			console.log( '- Error while posting statistics to https://bots.ondiscord.xyz/bots/' + process.env.bot + ': ' + error );
+		} );
+		if ( process.env.dbltoken ) got.post( 'https://discordbotlist.com/api/v1/bots/' + process.env.bot + '/stats', {
+			headers: {
+				Authorization: process.env.dbltoken
+			},
+			json: {
+				guilds: guildCount
+			},
+			responseType: 'json'
+		} ).catch( error => {
+			console.log( '- Error while posting statistics to https://discordbotlist.com/bots/' + process.env.bot + ': ' + error );
+		} );
+	}, error => console.log( '- Error while getting the guild count: ' + error ) );
+}
+
+
 async function graceful(signal) {
 async function graceful(signal) {
 	console.log( '- ' + signal + ': Disabling respawn...' );
 	console.log( '- ' + signal + ': Disabling respawn...' );
 	manager.respawn = false;
 	manager.respawn = false;