From 8bb0797b3abea40aee8d1af1db8e3bec58183482 Mon Sep 17 00:00:00 2001 From: CL TheDreWen Date: Wed, 29 Oct 2025 09:19:34 +0100 Subject: [PATCH] Status update for server statuses. --- README.md | 5 +++-- src/index.ts | 2 ++ src/services/status.service.ts | 22 +++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 60828ae..1fdf430 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,12 @@ A status bot and other features for protojx. | Description | Status | |-------------|--------| -| /status command | ✅ | -| Number of services down in the bot's status. | ➖ | +| /status command | 🌐 | +| Number of services down in the bot's status. | ✅ | | Notification system in case of downtime. | ➖ | | Deployment workflow on Raspberry Pi. | ➖ | +- 🌐 -> In production - ✅ -> Done - 🚧 -> Under development - ➖ -> Not started diff --git a/src/index.ts b/src/index.ts index b102b8b..08e35a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from "dis import { configDotenv } from "dotenv"; import path from "path"; import fs from "fs"; +import statusService from "./services/status.service"; configDotenv(); @@ -61,6 +62,7 @@ client.on(Events.InteractionCreate, async interaction => { client.once(Events.ClientReady, readyClient => { console.log(`Ready! Logged in as ${readyClient.user.tag}`); + statusService.setClient(client); }); client.login(process.env.TOKEN) \ No newline at end of file diff --git a/src/services/status.service.ts b/src/services/status.service.ts index 1953998..afe5e34 100644 --- a/src/services/status.service.ts +++ b/src/services/status.service.ts @@ -1,5 +1,6 @@ import ping from "ping"; import * as cron from 'cron'; +import { ActivityType, Client } from "discord.js"; export class StatusService { @@ -59,11 +60,14 @@ export class StatusService { type: 'ping' } ] + + private client : Client|null = null; constructor() { const cronJob = new cron.CronJob('*/2 * * * *', async () => { try { await this.fetch(); + await this.updateClientStatus(); console.log('Status check completed at:', new Date().toISOString()); } catch (error) { console.error('Error during status check:', error); @@ -71,9 +75,25 @@ export class StatusService { }); cronJob.start(); console.log('Status monitoring started - checking every 2 minutes'); + } + + setClient(client : Client) { + this.client = client; + this.client.user?.setActivity({name: '💭 Server load and status...'}) } - async fetch(max = 1): Promise { + private async updateClientStatus() { + if(this.client) { + const hosts = this.hosts.length; + const hostsAlive = this.hosts.filter((h) => h.alive).length; + + this.client.user?.setActivity({name: ( + hosts == hostsAlive ? '✅ All services are online.' : `📛 ${hosts - hostsAlive} service${hosts - hostsAlive > 1 ? 's' : ''} offline.` + ), type: ActivityType.Watching}); + } + } + + private async fetch(max = 1): Promise { const hosts = this.hosts.filter((value, index) => index < max * 10 && index >= (max - 1) * 10); async function fetchAlive(host: Host) {