diff --git a/.env.example b/.env.example index a8c32bf..c354245 100644 --- a/.env.example +++ b/.env.example @@ -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= diff --git a/src/events/client/ready.js b/src/events/client/ready.js index 59e249d..16aa507 100644 --- a/src/events/client/ready.js +++ b/src/events/client/ready.js @@ -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, @@ -17,5 +18,8 @@ module.exports = { } ] }); + + // Post bot stats to bot lists in production + process.env.NODE_ENV === 'production' ? postBotStats(client) : null; } }; diff --git a/src/utils/postBotStats.js b/src/utils/postBotStats.js new file mode 100644 index 0000000..5aada25 --- /dev/null +++ b/src/utils/postBotStats.js @@ -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'); + } +};