mirror of
https://github.com/thedrewen/protojx-manager.git
synced 2026-03-21 09:48:56 +01:00
Status update for server statuses.
This commit is contained in:
@@ -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)
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user