CircleBot/includes/class_server.js

77 lines
3.3 KiB
JavaScript

const ChildProcess = require('child_process');
const { Client, GatewayIntentBits, Partials, EmbedBuilder } = require('discord.js');
const Config = require('./config/config');
class Server {
slug = "";
name = "";
platformURL = "";
mcVersion = "";
iconURL = "";
description = "";
addressPrimary = "";
addressSecondary = "";
queryPort = 0;
rconAddress = "";
rconPort = 0;
rconPassword = "";
rconTPSCommand = "";
rconOnlineCommand = "";
active = false;
constructor(serverObj) {
if ( serverObj.slug ) this.slug = serverObj.slug;
if ( serverObj.name ) this.name = serverObj.name;
if ( serverObj.platformURL ) this.platformURL = serverObj.platformURL;
if ( serverObj.mcVersion ) this.mcVersion = serverObj.mcVersion;
if ( serverObj.iconURL ) this.iconURL = serverObj.iconURL;
if ( serverObj.description ) this.description = serverObj.description;
if ( serverObj.addressPrimary ) this.addressPrimary = serverObj.addressPrimary;
if ( serverObj.addressSecondary ) this.addressSecondary = serverObj.addressSecondary;
if ( serverObj.queryPort ) this.queryPort = serverObj.queryPort;
if ( serverObj.rconAddress ) this.rconAddress = serverObj.rconAddress;
if ( serverObj.rconPort ) this.rconPort = serverObj.rconPort;
if ( serverObj.rconPassword ) this.rconPassword = serverObj.rconPassword;
if ( serverObj.rconTPSCommand ) this.rconTPSCommand = serverObj.rconTPSCommand;
if ( serverObj.rconOnlineCommand ) this.rconOnlineCommand = serverObj.rconOnlineCommand;
if ( serverObj.active ) this.active = serverObj.active;
}
hostIsAlive() {
if ( !this.active || (this.rconAddress == "") ) return false;
if ( Config.ONLINE_CHECK == false ) return true;
try {
let res = ChildProcess.execSync('ping -c 1 -W 0.25 -q ' + this.rconAddress);
return true;
} catch (e) {
console.log("Host for " + this.name + " at " + this.rconAddress + " not available");
return false;
}
}
sendDetails(channel, hostStatus, serverStatus, serverTPS, queryStatus = {}) {
let embed = new EmbedBuilder()
.setTitle(this.name + " *(" + this.slug + ")*")
.setDescription(this.description)
.setURL(this.platformURL)
.setThumbnail(this.iconURL)
.setColor(0xffaaaa)
.addFields(
{ name: "**MC Version:**", value: this.mcVersion, inline: true },
{ name: "**Server Address:**", value: this.addressPrimary, inline: true },
{ name: "**Server Alternate:**", value: this.addressSecondary, inline: true },
{ name: "**Host Status:**", value: hostStatus, inline: true },
{ name: "**Server Status:**", value: serverStatus, inline: true },
{ name: "**Server TPS:**", value: serverTPS, inline: true }
)
if ( queryStatus.players ) {
let players = (queryStatus.players.length > 0) ? queryStatus.players.join(', ') : "None";
embed.addFields({ name: "**Online Players (" + queryStatus.online_players + "):**", value: players, inline: false });
}
channel.send({ embeds: [embed] });
}
}
module.exports = Server;