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": ""
},
"stormworks": {
"updateInterval": 10,
"lowTPS": 45,
"servers": [
{
"ip": "127.0.0.1",
"port": 25565,
"messageId": "1234567890123456789",
"messageId": "this is optional, if you want to use an already existing message, if not, delete this line",
"channelId": "9876543210987654321"
}
]

View file

@ -1,6 +1,7 @@
const config = require('./config.json');
const colors = require('colors');
const Discord = require('discord.js');
const fs = require('fs');
const rest = new Discord.REST({
version: '10'
}).setToken(config.discord.token);
@ -137,8 +138,15 @@ 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) {
if (!serverEmbeds[`${addr}:${port}`]) {
serverEmbeds[`${addr}:${port}`] = {
"title": "Unknown",
@ -175,7 +183,7 @@ function updateStatus(addr, port, msg) {
"timestamp": new Date()
};
}
if(!serverStatus[`${addr}:${port}`]) {
if (!serverStatus[`${addr}:${port}`]) {
serverStatus[`${addr}:${port}`] = {
"status": false,
"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 (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.`);
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
startTimeout(addr, port, msg);
return;
}
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online.`);
@ -245,25 +250,16 @@ function updateStatus(addr, port, msg) {
embeds: [embed]
}).then(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is online, edited embed.`);
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
startTimeout(addr, port, msg);
}).catch((err) => {
console.log(`${colors.red("[ERROR]")} ${err}`);
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
startTimeout(addr, port, msg);
});
} else {
// If the server was already offline, don't edit the embed
if (!serverStatus[`${addr}:${port}`].status) {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is offline, but was already offline.`);
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
startTimeout(addr, port, msg);
return;
}
serverStatus[`${addr}:${port}`].status = false;
@ -309,16 +305,10 @@ function updateStatus(addr, port, msg) {
"embeds": [serverEmbeds[`${addr}:${port}`]]
}).then((msg) => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} ${addr}:${port} is offline, edited embed.`);
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
startTimeout(addr, port, msg);
}).catch((err) => {
console.log(`${colors.red("[ERROR]")} ${err}`);
setTimeout(() => {
console.log(`${colors.magenta(`[DEBUG ${new Date()}]`)} Updating ${addr}:${port}`);
updateStatus(addr, port, msg);
}, 5000)
startTimeout(addr, port, msg);
});
}
})
@ -357,11 +347,28 @@ client.on("ready", async () => {
console.log(`${colors.cyan("[INFO]")} Startup took ${colors.green((Date.now() - initTime) / 1000)} seconds.`)
config.stormworks.servers.forEach((server) => {
if (!server.channelId) throw new Error("Channel ID not set in config!!!");
if (!server.messageId) {
// 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) => {
// channel.messages.fetch("1108860190935232592").then((message) => {