diff --git a/src/botListPoster.ts b/src/botListPoster.ts new file mode 100644 index 0000000..61e0b82 --- /dev/null +++ b/src/botListPoster.ts @@ -0,0 +1,26 @@ +import { botId, log, LT } from '../deps.ts'; +import config from '../config.ts'; + +// updateListStatistics(bot ID, current guild count) returns nothing, posts to botlists +// Sends the current server count to all bot list sites we are listed on +export const updateBotListStatistics = (serverCount: number): void => { + config.botLists.forEach(async (botList) => { + try { + log(LT.LOG, `Updating statistics for ${JSON.stringify(botList)}`); + if (botList.enabled) { + const tempHeaders = new Headers(); + tempHeaders.append(botList.headers[0].header, botList.headers[0].value); + tempHeaders.append('Content-Type', 'application/json'); + // ?{} is a template used in config, just need to replace it with the real value + const response = await fetch(botList.apiUrl.replace('?{bot_id}', botId.toString()), { + 'method': 'POST', + 'headers': tempHeaders, + 'body': JSON.stringify(botList.body).replace('"?{server_count}"', serverCount.toString()), // ?{server_count} needs the "" removed from around it aswell to make sure its sent as a number + }); + log(LT.INFO, `Posted server count to ${botList.name}. Results: ${JSON.stringify(response)}`); + } + } catch (err) { + log(LT.ERROR, `Failed to update statistics for ${botList.name} | Error: ${err.name} - ${err.message}`) + } + }); +}; diff --git a/src/events/ready.ts b/src/events/ready.ts index 2a3ba3d..b328088 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -6,10 +6,12 @@ import { ActiveEvent } from '../types/commandTypes.ts'; import utils from '../utils.ts'; import { dbClient, queries } from '../db.ts'; import { deleteEvent, handleFailures, lockEvent, notifyEventMembers, tenMinutes } from '../notificationSystem.ts'; +import { updateBotListStatistics } from '../botListPoster.ts'; // Storing intervalIds in case bot soft reboots to prevent multiple of these intervals from stacking let notificationIntervalId: number; let botStatusIntervalId: number; +let botListPosterIntervalId: number; export const ready = (rawBot: Bot) => { const bot = rawBot as BotWithCache; @@ -60,6 +62,13 @@ export const ready = (rawBot: Bot) => { eventFailuresToHandle?.rows?.forEach((event) => handleFailures(bot, event as ActiveEvent)); }, 30000); + // Interval to handle updating botList statistics + if (botListPosterIntervalId) clearInterval(botListPosterIntervalId) + LOCALMODE ? log(LT.INFO, 'updateListStatistics not running') : botListPosterIntervalId = setInterval(() => { + log(LT.LOG, 'Updating all bot lists statistics'); + updateBotListStatistics(bot.guilds.size + bot.dispatchedGuildIds.size); + }, 86400000); + // setTimeout added to make sure the startup message does not error out setTimeout(() => { LOCALMODE && bot.helpers.editBotMember(config.devServer, { nick: `LOCAL - ${config.name}` });