CircleBot/includes/class_server.js

92 lines
3.7 KiB
JavaScript

const Ping = require('net-ping');
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() {
try {
if ( !this.active || (this.rconAddress == "") ) return false;
let session = Ping.createSession({retries: 1, timeout: 250});
let result = await session.pingHost(this.rconAddress, function(error, target) {
if ( error ) {
return false;
} else {
return true;
}
});
return result;
} catch (e) {
console.log(e);
return false;
}
}
static sendOnline(status, channel) {
let playerCount = 0;
let embed = new Discord.MessageEmbed()
.setThumbnail("https://www.circlecraft.info/images/circlecraft_discord.png")
.setColor(0x44ff44);
status.forEach(function (s) {
if ( !s.tested ) return;
if ( s.players.length > 0 ) embed.addField("**" + s.name + "**:", s.players.join(', '), false);
});
embed.setTitle("Players Currently Online: " + playerCount);
channel.send(embed);
}
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;