Status update for server statuses.

This commit is contained in:
2025-10-29 09:19:34 +01:00
parent 25fd742cfe
commit 8bb0797b3a
3 changed files with 26 additions and 3 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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<Host[]> {
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<Host[]> {
const hosts = this.hosts.filter((value, index) => index < max * 10 && index >= (max - 1) * 10);
async function fetchAlive(host: Host) {