From 026b81645f5195936124c307790ce911dd5861df Mon Sep 17 00:00:00 2001 From: janderedev Date: Sat, 9 Apr 2022 14:38:27 +0200 Subject: [PATCH] add bot status, fetch all users on startup --- bot/src/bot/modules/bot_status.ts | 52 +++++++++++++++++++++++++++++++ bot/src/bot/modules/fetch_all.ts | 19 +++++++++++ bot/src/index.ts | 2 ++ 3 files changed, 73 insertions(+) create mode 100644 bot/src/bot/modules/bot_status.ts create mode 100644 bot/src/bot/modules/fetch_all.ts diff --git a/bot/src/bot/modules/bot_status.ts b/bot/src/bot/modules/bot_status.ts new file mode 100644 index 0000000..1f4cf1c --- /dev/null +++ b/bot/src/bot/modules/bot_status.ts @@ -0,0 +1,52 @@ +import axios from "axios"; +import { client, dbs } from "../.."; +import logger from "../logger"; + +(async () => { + if (!client.user) await new Promise(r => client.once('ready', () => r())); + const interval = process.env['BOT_STATUS_INTERVAL']; // In seconds + const statuses = process.env['BOT_STATUS'] + ?.split('||') + .map(text => text.trim()); + + if (statuses?.length && interval) { + let i = 0; + + const update = async () => { + try { + const statusText = statuses[i] + .replace('{{servers}}', `${client.servers.size}`) + .replace('{{users}}', `${client.users.size}`) + .replace('{{infractions_total}}', `${await dbs.INFRACTIONS.count({})}`) + .replace('{{ping_ms}}', `${client.websocket.ping ?? -1}`); + + await setStatus(statusText, 'Online'); + logger.debug(`Bot status updated`); + + i++; + if (i >= statuses.length) i = 0; + } catch(e) { + console.error(`Failed to update bot status: ${e}`); + } + } + + update(); + setInterval(update, parseInt(interval) * 1000); + } +})(); + +async function setStatus(text: string, presence: 'Online'|'Idle'|'Busy'|'Invisible') { + await axios.patch( + `${client.apiURL}/users/@me`, + { + status: { text, presence } + }, + { + headers: { + 'x-bot-token': process.env['BOT_TOKEN']! + } + } + ); +} + +export { setStatus } diff --git a/bot/src/bot/modules/fetch_all.ts b/bot/src/bot/modules/fetch_all.ts new file mode 100644 index 0000000..56531f1 --- /dev/null +++ b/bot/src/bot/modules/fetch_all.ts @@ -0,0 +1,19 @@ +import { client } from "../.."; +import logger from "../logger"; + +// Fetch all known users on bot startup. + +(async () => { + if (!client.user) await new Promise(r => client.once('ready', () => r())); + + logger.info(`Starting to fetch users in ${client.servers.size} servers.`); + + const promises: Promise[] = []; + for (const server of client.servers) { + promises.push(server[1].fetchMembers()); + } + + const res = await Promise.allSettled(promises); + logger.done(`Downloaded all users from ${res.filter(r => r.status == 'fulfilled').length} servers ` + + `with ${res.filter(r => r.status == 'rejected').length} errors. Cache size: ${client.users.size}`); +})(); diff --git a/bot/src/index.ts b/bot/src/index.ts index 2722e22..6b3a2b9 100644 --- a/bot/src/index.ts +++ b/bot/src/index.ts @@ -50,4 +50,6 @@ export { client, dbs } import('./bot/modules/user_scan'); import('./bot/modules/api_communication'); import('./bot/modules/metrics'); + import('./bot/modules/bot_status'); + import('./bot/modules/fetch_all'); })();