diff --git a/src/utils/other/postBotStats.ts b/src/utils/other/postBotStats.ts index 973c4b4b..c1eb1b0e 100644 --- a/src/utils/other/postBotStats.ts +++ b/src/utils/other/postBotStats.ts @@ -13,16 +13,16 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams) shardId: client.shard?.ids[0] }); - try { - if (client.shard?.ids[0] !== 0) { - return; - } + if (client.shard?.ids[0] !== 0) { + return; + } - let guildCount: number = 0; - let memberCount: number = 0; - const shardCount: number = client.shard.count; - const shardId: number = client.shard.ids[0]; + let guildCount: number = 0; + let memberCount: number = 0; + const shardCount: number = client.shard.count; + const shardId: number = client.shard.ids[0]; + try { logger.debug('Gathering data about guild and member count from shards...'); await client! .shard!.fetchClientValues('guilds.cache') @@ -40,129 +40,137 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams) .catch((error) => { logger.error(error, 'Failed to fetch client values from shards.'); }); + } catch (error) { + logger.error(error, 'Failed to gather data about guild and member count from shards.'); + } + + logger.info(`Starting to post bot stats with guild count ${guildCount} and member count ${memberCount}...`); - /* eslint-disable camelcase */ - const sites: PostBotStatsSite[] = [ - { - enabled: false, - hostname: 'top.gg', - path: `/api/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, - method: 'POST', - body: { - shard_id: shardId, - shard_count: shardCount, - server_count: guildCount - }, - token: process.env.BOTLIST_TOP_GG_API_TOKEN ?? '' + /* eslint-disable camelcase */ + const sites: PostBotStatsSite[] = [ + { + enabled: false, + hostname: 'top.gg', + path: `/api/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, + method: 'POST', + body: { + shard_id: shardId, + shard_count: shardCount, + server_count: guildCount }, - { - enabled: false, - hostname: 'discordbotlist.com', - path: `/api/v1/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, - method: 'POST', - body: { - shard_id: shardId, - users: memberCount, - guilds: guildCount - }, - token: process.env.BOTLIST_DISCORD_BOT_LIST_COM_API_TOKEN ?? '' + token: process.env.BOTLIST_TOP_GG_API_TOKEN ?? '' + }, + { + enabled: false, + hostname: 'discordbotlist.com', + path: `/api/v1/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, + method: 'POST', + body: { + shard_id: shardId, + users: memberCount, + guilds: guildCount }, - { - enabled: false, - hostname: 'discords.com', - path: `/bots/api/bot/${process.env.DISCORD_APPLICATION_ID}`, - method: 'POST', - body: { - server_count: guildCount - }, - token: process.env.BOTLIST_DISCORDS_COM_API_TOKEN ?? '' + token: process.env.BOTLIST_DISCORD_BOT_LIST_COM_API_TOKEN ?? '' + }, + { + enabled: false, + hostname: 'discords.com', + path: `/bots/api/bot/${process.env.DISCORD_APPLICATION_ID}`, + method: 'POST', + body: { + server_count: guildCount }, - { - enabled: false, - hostname: 'discord.bots.gg', - path: `/api/v1/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, - method: 'POST', - body: { - shardId: shardId, - shardCount: shardCount, - guildCount: guildCount - }, - token: process.env.BOTLIST_DISCORD_BOTS_GG_API_TOKEN ?? '' + token: process.env.BOTLIST_DISCORDS_COM_API_TOKEN ?? '' + }, + { + enabled: false, + hostname: 'discord.bots.gg', + path: `/api/v1/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, + method: 'POST', + body: { + shardId: shardId, + shardCount: shardCount, + guildCount: guildCount }, - { - enabled: false, - hostname: 'api.botlist.me', - path: `/api/v1/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, - method: 'POST', - body: { - shard_count: shardCount, - server_count: guildCount - }, - token: process.env.BOTLIST_BOTLIST_ME_API_TOKEN ?? '' + token: process.env.BOTLIST_DISCORD_BOTS_GG_API_TOKEN ?? '' + }, + { + enabled: false, + hostname: 'api.botlist.me', + path: `/api/v1/bots/${process.env.DISCORD_APPLICATION_ID}/stats`, + method: 'POST', + body: { + shard_count: shardCount, + server_count: guildCount }, - { - enabled: false, - hostname: 'api.discordlist.gg', - path: `/v0/bots/${process.env.DISCORD_APPLICATION_ID}/guilds`, - method: 'POST', - body: { - count: guildCount - }, - token: `Bearer ${process.env.BOTLIST_DISCORDLIST_GG_API_TOKEN}` + token: process.env.BOTLIST_BOTLIST_ME_API_TOKEN ?? '' + }, + { + enabled: false, + hostname: 'api.discordlist.gg', + path: `/v0/bots/${process.env.DISCORD_APPLICATION_ID}/guilds`, + method: 'POST', + body: { + count: guildCount }, - { - enabled: false, - hostname: 'api.discord-botlist.eu', - path: '/v1/update', - method: 'PATCH', - body: { - serverCount: guildCount - }, - token: `Bearer ${process.env.BOTLIST_DISCORD_BOTLIST_EU_API_TOKEN}` - } - ]; - - logger.info(`Starting to post bot stats with guild count ${guildCount} and member count ${memberCount}...`); - - const statusCodes: number[] = []; - - sites.map((site) => { - if (!site.enabled) { - // return if site is disabled - return; - } + token: `Bearer ${process.env.BOTLIST_DISCORDLIST_GG_API_TOKEN}` + }, + { + enabled: false, + hostname: 'api.discord-botlist.eu', + path: '/v1/update', + method: 'PATCH', + body: { + serverCount: guildCount + }, + token: `Bearer ${process.env.BOTLIST_DISCORD_BOTLIST_EU_API_TOKEN}` + } + ]; - const options: ClientRequestArgs = { - protocol: 'https:', - hostname: site.hostname, - port: 443, - path: site.path, - method: site.method, - headers: { - 'Content-Type': 'application/json', - Authorization: site.token + try { + const statusPromises = sites.map((site) => { + return new Promise((resolve) => { + if (!site.enabled) { + // return if site is disabled + resolve(null); + return; } - }; - const request: ClientRequest = https.request(options, (res) => { - if (typeof res.statusCode === 'number') { - res.statusCode === 200 - ? logger.debug(`Request to ${site.hostname}: statusCode: ${res.statusCode}`) - : logger.warn(`Request to ${site.hostname}: statusCode: ${res.statusCode}`); + const options: ClientRequestArgs = { + protocol: 'https:', + hostname: site.hostname, + port: 443, + path: site.path, + method: site.method, + headers: { + 'Content-Type': 'application/json', + Authorization: site.token + } + }; - statusCodes.push(res.statusCode); - } else { - logger.error( - `Request to ${site.hostname}: statusCode is undefined or not a number (${res.statusCode}).` - ); - } - }); + const request: ClientRequest = https.request(options, (res) => { + if (typeof res.statusCode === 'number') { + res.statusCode === 200 + ? logger.debug(`Request to ${site.hostname}: statusCode: ${res.statusCode}`) + : logger.warn(`Request to ${site.hostname}: statusCode: ${res.statusCode}`); + resolve(res.statusCode); + } else { + logger.error( + `Request to ${site.hostname}: statusCode is undefined or not a number (${res.statusCode}).` + ); + resolve(null); + } + }); - request.write(JSON.stringify(site.body)); - request.end(); + request.write(JSON.stringify(site.body)); + request.end(); + }); }); - logger.info(`Successfully posted bot stats with status codes ${statusCodes.join(', ')}.`); + const statusCodes = await Promise.all(statusPromises); + logger.info( + `Successfully posted bot stats with status codes ${statusCodes.filter((code) => code !== null).join(', ')}.` + ); } catch (error) { logger.error(error, 'Failed to post bot stats to sites.'); }