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

Commit

Permalink
feat: Automatically send guildCount stats to botlist sites on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Jul 17, 2023
1 parent 6d132c7 commit 547f3ca
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ DISCORD_CLIENT_ID=YOUR_DISCOR_BOT_CLIENT_ID
UV_THREADPOOL_SIZE=4
NODE_OPTIONS=
YOUTUBE_COOKIE=
BOTLIST_TOP_GG_API_TOKEN=
BOTLIST_DISCORD_BOT_LIST_COM_API_TOKEN=
BOTLIST_DISCORDS_COM_API_TOKEN=
BOTLIST_DISCORD_BOTS_GG_API_TOKEN=
BOTLIST_DISCORDLIST_GG_API_TOKEN=
4 changes: 4 additions & 0 deletions src/events/client/ready.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('node:path');
const logger = require(path.resolve('./src/services/logger.js'));
const { Events, ActivityType, PresenceUpdateStatus } = require('discord.js');
const { postBotStats } = require(path.resolve('./src/utils/postBotStats.js'));

module.exports = {
name: Events.ClientReady,
Expand All @@ -17,5 +18,8 @@ module.exports = {
}
]
});

// Post bot stats to bot lists in production
process.env.NODE_ENV === 'production' ? postBotStats(client) : null;
}
};
80 changes: 80 additions & 0 deletions src/utils/postBotStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const path = require('node:path');
const logger = require(path.resolve('./src/services/logger.js'));
const https = require('node:https');
require('dotenv').config();

exports.postBotStats = (client) => {
try {
const guildCount = client.guilds.cache.size;

/* eslint-disable camelcase */
const sites = [
{
name: 'top.gg',
hostname: 'top.gg',
path: `/api/bots/${process.env.DISCORD_CLIENT_ID}/stats`,
method: 'POST',
body: { server_count: guildCount },
token: process.env.BOTLIST_TOP_GG_API_TOKEN
},
{
name: 'discordbotlist.com',
hostname: 'discordbotlist.com',
path: `/api/v1/bots/${process.env.DISCORD_CLIENT_ID}/stats`,
method: 'POST',
body: { guilds: guildCount },
token: process.env.BOTLIST_DISCORD_BOT_LIST_COM_API_TOKEN
},
{
name: 'discords.com',
hostname: 'discords.com',
path: `/bots/api/bot/${process.env.DISCORD_CLIENT_ID}`,
method: 'POST',
body: { server_count: guildCount },
token: process.env.BOTLIST_DISCORDS_COM_API_TOKEN
},
{
name: 'discord.bots.gg',
hostname: 'discord.bots.gg',
path: `/api/v1/bots/${process.env.DISCORD_CLIENT_ID}/stats`,
method: 'POST',
body: { guildCount: guildCount },
token: process.env.BOTLIST_DISCORD_BOTS_GG_API_TOKEN
},
{
name: 'discordlist.gg',
hostname: 'api.discordlist.gg',
path: `/v0/bots/${process.env.DISCORD_CLIENT_ID}/guilds?count=${guildCount}`,
method: 'PUT',
body: {},
token: `Bearer ${process.env.BOTLIST_DISCORDLIST_GG_API_TOKEN}`
}
];

logger.info(`Posting stats to bot lists with guildCount ${guildCount}...`);
sites.map((site) => {
let options = {
protocol: 'https:',
hostname: site.hostname,
port: 443,
path: site.path,
method: site.method,
headers: {
'Content-Type': 'application/json',
Authorization: site.token
}
};

let request = https.request(options, (res) => {
res.statusCode === 200
? logger.info(`Request to ${site.name}: statusCode: ${res.statusCode}`)
: logger.warn(`Request to ${site.name}: statusCode: ${res.statusCode}`);
});

request.write(JSON.stringify(site.body));
request.end();
});
} catch (error) {
logger.error(error, 'Failed to post stats to bot lists');
}
};

0 comments on commit 547f3ca

Please # to comment.