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"
} }
] ]

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);
@ -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) { function updateStatus(addr, port, msg) {
if (!serverEmbeds[`${addr}:${port}`]) { if (!serverEmbeds[`${addr}:${port}`]) {
serverEmbeds[`${addr}:${port}`] = { serverEmbeds[`${addr}:${port}`] = {
"title": "Unknown", "title": "Unknown",
@ -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.`);
@ -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;
@ -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)
}); });
} }
}) })
@ -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) => {
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) => { client.channels.fetch(server.channelId).then((channel) => {
channel.messages.fetch(server.messageId).then((message) => { channel.messages.fetch(server.messageId).then((message) => {
updateStatus(server.ip, server.port, 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) => {