mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: Update postBotStats to output status codes from request
This commit is contained in:
1 parent
6d12735dc5
commit
9e20805529
1 file changed
+131
-123
+131
-123
@@ -13,16 +13,16 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
|
|||||||
shardId: client.shard?.ids[0]
|
shardId: client.shard?.ids[0]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
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];
|
|
||||||
|
|
||||||
logger.debug('Gathering data about guild and member count from shards...');
|
logger.debug('Gathering data about guild and member count from shards...');
|
||||||
await client!
|
await client!
|
||||||
.shard!.fetchClientValues('guilds.cache')
|
.shard!.fetchClientValues('guilds.cache')
|
||||||
@@ -40,129 +40,137 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
logger.error(error, 'Failed to fetch client values from shards.');
|
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.');
|
||||||
|
}
|
||||||
|
|
||||||
/* eslint-disable camelcase */
|
logger.info(`Starting to post bot stats with guild count ${guildCount} and member count ${memberCount}...`);
|
||||||
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 ?? ''
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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 ?? ''
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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 ?? ''
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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 ?? ''
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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 ?? ''
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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}`
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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}...`);
|
/* 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 ?? ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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 ?? ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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 ?? ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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 ?? ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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 ?? ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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}`
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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 statusCodes: number[] = [];
|
try {
|
||||||
|
const statusPromises = sites.map((site) => {
|
||||||
sites.map((site) => {
|
return new Promise<number | null>((resolve) => {
|
||||||
if (!site.enabled) {
|
if (!site.enabled) {
|
||||||
// return if site is disabled
|
// return if site is disabled
|
||||||
return;
|
resolve(null);
|
||||||
}
|
return;
|
||||||
|
|
||||||
const options: ClientRequestArgs = {
|
|
||||||
protocol: 'https:',
|
|
||||||
hostname: site.hostname,
|
|
||||||
port: 443,
|
|
||||||
path: site.path,
|
|
||||||
method: site.method,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: site.token
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const request: ClientRequest = https.request(options, (res) => {
|
const options: ClientRequestArgs = {
|
||||||
if (typeof res.statusCode === 'number') {
|
protocol: 'https:',
|
||||||
res.statusCode === 200
|
hostname: site.hostname,
|
||||||
? logger.debug(`Request to ${site.hostname}: statusCode: ${res.statusCode}`)
|
port: 443,
|
||||||
: logger.warn(`Request to ${site.hostname}: statusCode: ${res.statusCode}`);
|
path: site.path,
|
||||||
|
method: site.method,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: site.token
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
statusCodes.push(res.statusCode);
|
const request: ClientRequest = https.request(options, (res) => {
|
||||||
} else {
|
if (typeof res.statusCode === 'number') {
|
||||||
logger.error(
|
res.statusCode === 200
|
||||||
`Request to ${site.hostname}: statusCode is undefined or not a number (${res.statusCode}).`
|
? 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) {
|
} catch (error) {
|
||||||
logger.error(error, 'Failed to post bot stats to sites.');
|
logger.error(error, 'Failed to post bot stats to sites.');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user