CircleBot/includes/class_server.js

73 lines
3.0 KiB
JavaScript

const ChildProcess = require('child_process');
const Discord = require('discord.js');
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;
}
async hostIsAlive() {
if ( !this.active || (this.rconAddress == "") ) return false;
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 Discord.MessageEmbed()
.setTitle(this.name + " *(" + this.slug + ")*")
.setDescription(this.description)
.setURL(this.platformURL)
.setThumbnail(this.iconURL)
.setColor(0xffaaaa)
.addField("**MC Version:**", this.mcVersion, true)
.addField("**Server Address:**", this.addressPrimary, true)
.addField("**Server Alternate:**", this.addressSecondary, true)
.addField("**Host Status:**", hostStatus, true)
.addField("**Server Status:**", serverStatus, true)
.addField("**Server TPS:**", serverTPS, true);
if ( queryStatus.players ) {
let players = (queryStatus.players.length > 0) ? queryStatus.players.join(', ') : "None";
embed.addField("**Online Players (" + queryStatus.online_players + "):**", players, false);
}
channel.send(embed);
}
}
module.exports = Server;