Add bot list stat posting system

This commit is contained in:
Ean Milligan (Bastion) 2023-05-01 13:26:25 -04:00
parent ce40b52faa
commit 0926a7df09
2 changed files with 35 additions and 0 deletions

26
src/botListPoster.ts Normal file
View File

@ -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}`)
}
});
};

View File

@ -6,10 +6,12 @@ import { ActiveEvent } from '../types/commandTypes.ts';
import utils from '../utils.ts'; import utils from '../utils.ts';
import { dbClient, queries } from '../db.ts'; import { dbClient, queries } from '../db.ts';
import { deleteEvent, handleFailures, lockEvent, notifyEventMembers, tenMinutes } from '../notificationSystem.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 // Storing intervalIds in case bot soft reboots to prevent multiple of these intervals from stacking
let notificationIntervalId: number; let notificationIntervalId: number;
let botStatusIntervalId: number; let botStatusIntervalId: number;
let botListPosterIntervalId: number;
export const ready = (rawBot: Bot) => { export const ready = (rawBot: Bot) => {
const bot = rawBot as BotWithCache; const bot = rawBot as BotWithCache;
@ -60,6 +62,13 @@ export const ready = (rawBot: Bot) => {
eventFailuresToHandle?.rows?.forEach((event) => handleFailures(bot, event as ActiveEvent)); eventFailuresToHandle?.rows?.forEach((event) => handleFailures(bot, event as ActiveEvent));
}, 30000); }, 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 added to make sure the startup message does not error out
setTimeout(() => { setTimeout(() => {
LOCALMODE && bot.helpers.editBotMember(config.devServer, { nick: `LOCAL - ${config.name}` }); LOCALMODE && bot.helpers.editBotMember(config.devServer, { nick: `LOCAL - ${config.name}` });