From e45113f07823f2ed7cecaf39da11e1e0111c7059 Mon Sep 17 00:00:00 2001 From: MCHost Date: Wed, 2 Oct 2024 00:06:33 -0400 Subject: [PATCH] add checks to create links --- package.json | 1 + public_bot.js | 98 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 9289f77..edaa0ab 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "cmd-promise": "^1.2.0", "discord.js": "^14.16.3", "jsonfile": "^6.1.0", "mymc-lib": "^1.1.0" diff --git a/public_bot.js b/public_bot.js index 4cc8de1..2e2fcce 100644 --- a/public_bot.js +++ b/public_bot.js @@ -3,6 +3,7 @@ import jsonfile from 'jsonfile'; import MyMCLib from 'mymc-lib'; import unirest from 'unirest'; import { readFileSync } from 'fs'; +import cmd from 'cmd-promise'; // Paths to config and tokens files const tokensFile = './tokens.json'; @@ -29,9 +30,9 @@ function saveTokens(tokens) { // Automatically request a new token if it doesn't exist or is invalid async function fetchAndSaveToken(userId, interaction) { return unirest - .post(config.endpoint) + .post(config.endpoint.toString()) .headers({ 'Accept': 'application/json', 'Content-Type': 'application/json' }) - .send({ "username": `mc_${userId}`, "password": config.password}) + .send({ "username": `mc_${userId}`, "password": config.password.toString()}) .then((tokenInfo) => { const tokens = loadTokens(); tokens[userId] = tokenInfo.body.token; // Save the new token @@ -251,15 +252,92 @@ client.on('interactionCreate', async interaction => { handleResponse(restartResult, interaction); break; - case 'create-link': - const customLinkResult = await handleApiCall(() => MyMC.createMyLink(), userId, interaction); - handleResponse(customLinkResult, interaction); - break; + case 'create-link': + try { + // Check if the server is running + const runningCheck = await cmd(`node /home/mchost/scripts/docker_exec.js mc_${interaction.user.id} "/" "echo test"`); + console.log(runningCheck.stdout); + + if (runningCheck.stdout.includes("not running")) { + const response = { + success: false + }; + return sendSexyEmbed("Server Booted", "Please use /start-server to boot the server.", interaction) + } + + // Check if the server is online + const out = await cmd(`sh /home/mchost/scripts/check_online.sh mc_${interaction.user.id}`); + console.log(out.stdout); + + // Assuming out.stdout is expected to be a string; '0' indicates still booting + if (out.stdout.trim() === '0') { + const response = { + success: false + }; + return sendSexyEmbed("Still Booting", "Please wait one minute and try again.", interaction) + } + + // Create custom link if checks pass + const customLinkResult = await handleApiCall(() => MyMC.createMyLink(), userId, interaction); + handleResponse(customLinkResult, interaction); + + } catch (error) { + console.error('Error during create-link command:', error); + const response = { + success: false, + fields: [ + { name: "Error", value: "An error occurred while processing your request." }, + { name: "Suggestion", value: "Please try again later." } + ] + }; + handleResponse(response, interaction); + } + break; + + - case 'create-sftp-link': - const sftpLinkResult = await handleApiCall(() => MyMC.createLinkSFTP(), userId, interaction); - handleResponse(sftpLinkResult, interaction, true); - break; + case 'create-sftp-link': + try { + // Check if the server is running + const runningCheck = await cmd(`node /home/mchost/scripts/docker_exec.js mc_${interaction.user.id} "/" "echo test"`); + console.log(runningCheck.stdout); + + if (runningCheck.stdout.includes("not running")) { + const response = { + success: false + }; + return sendSexyEmbed("Server Booted", "Please use /start-server to boot the server.", interaction) + } + + // Check if the server is online + const out = await cmd(`sh /home/mchost/scripts/check_online.sh mc_${interaction.user.id}`); + console.log(out.stdout); + + // Assuming out.stdout is expected to be a string; '0' indicates still booting + if (out.stdout.trim() === '0') { + const response = { + success: false + }; + return sendSexyEmbed("Still Booting", "Please wait one minute and try again.", interaction) + } + + // Create SFTP link if checks pass + const sftpLinkResult = await handleApiCall(() => MyMC.createLinkSFTP(), userId, interaction); + handleResponse(sftpLinkResult, interaction, true); + + } catch (error) { + console.error('Error during create-sftp-link command:', error); + const response = { + success: false, + fields: [ + { name: "Error", value: "An error occurred while processing your request." }, + { name: "Suggestion", value: "Please try again later." } + ] + }; + handleResponse(response, interaction, true); + } + break; + case 'get-connection-hash': const hash = await handleApiCall(() => MyMC.getConnectionHash(), userId, interaction);