add checks to create links

This commit is contained in:
MCHost 2024-10-02 00:06:33 -04:00
parent 78d976d7f9
commit e45113f078
2 changed files with 89 additions and 10 deletions

View File

@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"cmd-promise": "^1.2.0",
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"jsonfile": "^6.1.0", "jsonfile": "^6.1.0",
"mymc-lib": "^1.1.0" "mymc-lib": "^1.1.0"

View File

@ -3,6 +3,7 @@ import jsonfile from 'jsonfile';
import MyMCLib from 'mymc-lib'; import MyMCLib from 'mymc-lib';
import unirest from 'unirest'; import unirest from 'unirest';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import cmd from 'cmd-promise';
// Paths to config and tokens files // Paths to config and tokens files
const tokensFile = './tokens.json'; const tokensFile = './tokens.json';
@ -29,9 +30,9 @@ function saveTokens(tokens) {
// Automatically request a new token if it doesn't exist or is invalid // Automatically request a new token if it doesn't exist or is invalid
async function fetchAndSaveToken(userId, interaction) { async function fetchAndSaveToken(userId, interaction) {
return unirest return unirest
.post(config.endpoint) .post(config.endpoint.toString())
.headers({ 'Accept': 'application/json', 'Content-Type': 'application/json' }) .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) => { .then((tokenInfo) => {
const tokens = loadTokens(); const tokens = loadTokens();
tokens[userId] = tokenInfo.body.token; // Save the new token tokens[userId] = tokenInfo.body.token; // Save the new token
@ -252,15 +253,92 @@ client.on('interactionCreate', async interaction => {
break; break;
case 'create-link': 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); const customLinkResult = await handleApiCall(() => MyMC.createMyLink(), userId, interaction);
handleResponse(customLinkResult, 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; break;
case 'create-sftp-link': 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); const sftpLinkResult = await handleApiCall(() => MyMC.createLinkSFTP(), userId, interaction);
handleResponse(sftpLinkResult, interaction, true); 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; break;
case 'get-connection-hash': case 'get-connection-hash':
const hash = await handleApiCall(() => MyMC.getConnectionHash(), userId, interaction); const hash = await handleApiCall(() => MyMC.getConnectionHash(), userId, interaction);
handleResponse(hash, interaction, true); handleResponse(hash, interaction, true);