It can automatically make the messages now :)

This commit is contained in:
Christopher Cookman 2024-02-23 20:25:13 -07:00
parent a5dd7ff20d
commit 7f95b96e6c
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42
2 changed files with 113 additions and 105 deletions

View file

@ -3,12 +3,13 @@
"token": "" "token": ""
}, },
"stormworks": { "stormworks": {
"updateInterval": 10,
"lowTPS": 45, "lowTPS": 45,
"servers": [ "servers": [
{ {
"ip": "127.0.0.1", "ip": "127.0.0.1",
"port": 25565, "port": 25565,
"messageId": "1234567890123456789", "messageId": "this is optional, if you want to use an already existing message, if not, delete this line",
"channelId": "9876543210987654321" "channelId": "9876543210987654321"
} }
] ]

215
index.js
View file

@ -1,6 +1,7 @@
const config = require('./config.json'); const config = require('./config.json');
const colors = require('colors'); const colors = require('colors');
const Discord = require('discord.js'); const Discord = require('discord.js');
const fs = require('fs');
const rest = new Discord.REST({ const rest = new Discord.REST({
version: '10' version: '10'
}).setToken(config.discord.token); }).setToken(config.discord.token);
@ -15,7 +16,7 @@ function splitKeyword(keyword) {
switch (data[1]) { switch (data[1]) {
case "0": case "0":
dlcString = "None" dlcString = "None"
dlcEmoji = ":x:" dlcEmoji = ":x:"
break; break;
case "1": case "1":
dlcString = "Weapons" dlcString = "Weapons"
@ -137,36 +138,43 @@ function checkServer(address) {
}); });
} }
function startTimeout(addr, port, msg) {
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, config.stormworks.updateInterval * 1000);
}
function updateStatus(addr, port, msg) { function updateStatus(addr, port, msg) {
if (!serverEmbeds[`${addr}:${port}`]) { if (!serverEmbeds[`${addr}:${port}`]) {
serverEmbeds[`${addr}:${port}`] = { serverEmbeds[`${addr}:${port}`] = {
"title": "Unknown", "title": "Unknown",
"fields": [{ "fields": [{
"name": "Status", "name": "Status",
"value": `<:lowtps:1108862303618728108>: \`Unknown\``, "value": `<:lowtps:1108862303618728108>: \`Unknown\``,
"inline": true "inline": true
}, },
{ {
"name": "Version", "name": "Version",
"value": `?`, "value": `?`,
"inline": true "inline": true
}, },
{ {
"name": "DLC", "name": "DLC",
"value": `?`, "value": `?`,
"inline": true "inline": true
}, },
{ {
"name": "TPS", "name": "TPS",
"value": `?`, "value": `?`,
"inline": true "inline": true
}, },
{ {
"name": "Players", "name": "Players",
"value": `?/?`, "value": `?/?`,
"inline": true "inline": true
} }
], ],
"color": 0x00ffff, "color": 0x00ffff,
"footer": { "footer": {
@ -175,7 +183,7 @@ function updateStatus(addr, port, msg) {
"timestamp": new Date() "timestamp": new Date()
}; };
} }
if(!serverStatus[`${addr}:${port}`]) { if (!serverStatus[`${addr}:${port}`]) {
serverStatus[`${addr}:${port}`] = { serverStatus[`${addr}:${port}`] = {
"status": false, "status": false,
"error": "Could not connect to server", "error": "Could not connect to server",
@ -198,10 +206,7 @@ function updateStatus(addr, port, msg) {
// If none of the info has changed, don't edit the embed // If none of the info has changed, don't edit the embed
if (data.name == serverStatus[`${addr}:${port}`].name && data.version == serverStatus[`${addr}:${port}`].version && data.dlc == serverStatus[`${addr}:${port}`].dlc && data.tps == serverStatus[`${addr}:${port}`].tps && data.players == serverStatus[`${addr}:${port}`].players && data.maxPlayers == serverStatus[`${addr}:${port}`].maxPlayers) { if (data.name == serverStatus[`${addr}:${port}`].name && data.version == serverStatus[`${addr}:${port}`].version && data.dlc == serverStatus[`${addr}:${port}`].dlc && data.tps == serverStatus[`${addr}:${port}`].tps && data.players == serverStatus[`${addr}:${port}`].players && data.maxPlayers == serverStatus[`${addr}:${port}`].maxPlayers) {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online, but nothing has changed.`); console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online, but nothing has changed.`);
setTimeout(() => { startTimeout(addr, port, msg);
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
return; return;
} }
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online.`); console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online.`);
@ -209,30 +214,30 @@ function updateStatus(addr, port, msg) {
embed = { embed = {
"title": data.name, "title": data.name,
"fields": [{ "fields": [{
"name": "Status", "name": "Status",
"value": `${data.tps <= config.stormworks.lowTPS ? "<:lowtps:1108862303618728108>: `TPS Low`" : "<:online:1108862127021756457> `Online`"}`, "value": `${data.tps <= config.stormworks.lowTPS ? "<:lowtps:1108862303618728108>: `TPS Low`" : "<:online:1108862127021756457> `Online`"}`,
"inline": true "inline": true
}, },
{ {
"name": "Version", "name": "Version",
"value": `${data.version}`, "value": `${data.version}`,
"inline": true "inline": true
}, },
{ {
"name": "DLC", "name": "DLC",
"value": `${data.dlcEmotes} ${data.dlcString}`, "value": `${data.dlcEmotes} ${data.dlcString}`,
"inline": true "inline": true
}, },
{ {
"name": "TPS", "name": "TPS",
"value": `${data.tps}`, "value": `${data.tps}`,
"inline": true "inline": true
}, },
{ {
"name": "Players", "name": "Players",
"value": `${data.players}/${data.maxPlayers}`, "value": `${data.players}/${data.maxPlayers}`,
"inline": true "inline": true
} }
], ],
"color": data.tps <= config.stormworks.lowTPS ? 0xfff000 : 0x00ff00, "color": data.tps <= config.stormworks.lowTPS ? 0xfff000 : 0x00ff00,
"footer": { "footer": {
@ -245,25 +250,16 @@ function updateStatus(addr, port, msg) {
embeds: [embed] embeds: [embed]
}).then(() => { }).then(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online, edited embed.`); console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online, edited embed.`);
setTimeout(() => { startTimeout(addr, port, msg);
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
}).catch((err) => { }).catch((err) => {
console.log(`${colors.red("[ERROR]")} ${err}`); console.log(`${colors.red("[ERROR]")} ${err}`);
setTimeout(() => { startTimeout(addr, port, msg);
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
}); });
} else { } else {
// If the server was already offline, don't edit the embed // If the server was already offline, don't edit the embed
if (!serverStatus[`${addr}:${port}`].status) { if (!serverStatus[`${addr}:${port}`].status) {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is offline, but was already offline.`); console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is offline, but was already offline.`);
setTimeout(() => { startTimeout(addr, port, msg);
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
return; return;
} }
serverStatus[`${addr}:${port}`].status = false; serverStatus[`${addr}:${port}`].status = false;
@ -273,30 +269,30 @@ function updateStatus(addr, port, msg) {
embed = { embed = {
"title": data2.name, "title": data2.name,
"fields": [{ "fields": [{
"name": "Status", "name": "Status",
"value": `<:offline:1108862189793726485> \`Offline\``, "value": `<:offline:1108862189793726485> \`Offline\``,
"inline": true "inline": true
}, },
{ {
"name": "Version", "name": "Version",
"value": `${data2.version}`, "value": `${data2.version}`,
"inline": true "inline": true
}, },
{ {
"name": "DLC", "name": "DLC",
"value": `${data2.dlcEmotes} ${data2.dlcString}`, "value": `${data2.dlcEmotes} ${data2.dlcString}`,
"inline": true "inline": true
}, },
{ {
"name": "TPS", "name": "TPS",
"value": `0`, "value": `0`,
"inline": true "inline": true
}, },
{ {
"name": "Players", "name": "Players",
"value": `0/${data2.maxPlayers}`, "value": `0/${data2.maxPlayers}`,
"inline": true "inline": true
} }
], ],
"color": 0xff0000, "color": 0xff0000,
"footer": { "footer": {
@ -309,16 +305,10 @@ function updateStatus(addr, port, msg) {
"embeds": [serverEmbeds[`${addr}:${port}`]] "embeds": [serverEmbeds[`${addr}:${port}`]]
}).then((msg) => { }).then((msg) => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is offline, edited embed.`); console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is offline, edited embed.`);
setTimeout(() => { startTimeout(addr, port, msg);
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
}).catch((err) => { }).catch((err) => {
console.log(`${colors.red("[ERROR]")} ${err}`); console.log(`${colors.red("[ERROR]")} ${err}`);
setTimeout(() => { startTimeout(addr, port, msg);
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
}); });
} }
}) })
@ -342,8 +332,8 @@ client.on("ready", async () => {
// Register commands // Register commands
await rest.put( await rest.put(
Discord.Routes.applicationGuildCommands(client.user.id, guild.id), { Discord.Routes.applicationGuildCommands(client.user.id, guild.id), {
body: commands body: commands
}, },
); );
console.log(`${colors.cyan("[INFO]")} Successfully registered commands for ${colors.green(guild.name)}. Took ${colors.green((Date.now() - gStart) / 1000)} seconds.`); console.log(`${colors.cyan("[INFO]")} Successfully registered commands for ${colors.green(guild.name)}. Took ${colors.green((Date.now() - gStart) / 1000)} seconds.`);
}; };
@ -357,11 +347,28 @@ client.on("ready", async () => {
console.log(`${colors.cyan("[INFO]")} Startup took ${colors.green((Date.now() - initTime) / 1000)} seconds.`) console.log(`${colors.cyan("[INFO]")} Startup took ${colors.green((Date.now() - initTime) / 1000)} seconds.`)
config.stormworks.servers.forEach((server) => { config.stormworks.servers.forEach((server) => {
client.channels.fetch(server.channelId).then((channel) => { if (!server.channelId) throw new Error("Channel ID not set in config!!!");
channel.messages.fetch(server.messageId).then((message) => { if (!server.messageId) {
updateStatus(server.ip, server.port, message) // Lets send an empty embed in the channel and update the config file with the message ID. Then write the message ID to the config file
client.channels.fetch(server.channelId).then((channel) => {
channel.send({
embeds: [{
description: "Please Wait"
}]
}).then(async (msg) => {
server.messageId = msg.id;
fs.writeFileSync("./config.json", JSON.stringify(config, null, "\t"));
updateStatus(server.ip, server.port, msg)
});
}); });
});
} else {
client.channels.fetch(server.channelId).then((channel) => {
channel.messages.fetch(server.messageId).then((message) => {
updateStatus(server.ip, server.port, message)
});
});
}
}); });
// client.channels.fetch("1108126926045986856").then((channel) => { // client.channels.fetch("1108126926045986856").then((channel) => {
// channel.messages.fetch("1108860190935232592").then((message) => { // channel.messages.fetch("1108860190935232592").then((message) => {