Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: Update postBotStats to output status codes from request
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Sep 4, 2023
1 parent 6d12735 commit 9e20805
Showing 1 changed file with 125 additions and 117 deletions.
242 changes: 125 additions & 117 deletions src/utils/other/postBotStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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<number | null>((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.');
}
Expand Down

0 comments on commit 9e20805

Please # to comment.