From f6bf2f8616f81584530f5827f39ed4d1852d24f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zachary=20Gu=C3=A9not?= Date: Sun, 28 May 2023 04:01:54 +0200 Subject: [PATCH] Remise en forme du code AMP --- commands/global/amp.js | 92 ++++++++++++++++--------------------- utilsAMP/getInstances.js | 17 +++++++ utilsAMP/manageInstance.js | 34 ++++++++++++++ utilsAMP/restartInstance.js | 17 +++++++ 4 files changed, 107 insertions(+), 53 deletions(-) create mode 100644 utilsAMP/getInstances.js create mode 100644 utilsAMP/manageInstance.js create mode 100644 utilsAMP/restartInstance.js diff --git a/commands/global/amp.js b/commands/global/amp.js index d65c592..048dfa9 100644 --- a/commands/global/amp.js +++ b/commands/global/amp.js @@ -1,4 +1,4 @@ -const { SlashCommandBuilder } = require('discord.js') +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js') require('dotenv').config() require('require-all')(__dirname + '/../../utilsAMP') @@ -24,73 +24,59 @@ module.exports = { await interaction.deferReply({ ephemeral: true }) let result = await login(base_url, interaction.options) - //if (result) return await interaction.followUp({ content: `Sorry, something bad happened !` }) - //else return await interaction.followUp({ content: `You are successfully logged in as **${result}** !` }) - //await interaction.editReply(`You are successfully logged in as **${response.data.userInfo.Username}** !`) if (result.status === 'success') return await interaction.editReply(`You are successfully logged in as **${result.data.userInfo.Username}** !`) - else if (result.status === 'fail') { - - return await interaction.editReply(`Sorry, something bad happened ! (${result.data.Message})`) - } - else if (result.status === 'error') { - return await interaction.editReply(`Sorry, there has been an error ! (${result.data.error_code})`) - } + else if (result.status === 'fail') return await interaction.editReply(`Sorry, something bad happened ! (${result.data.Message})`) + else if (result.status === 'error') return await interaction.editReply(`Sorry, there has been an error ! (${result.data.error_code})`) } - /*else if (interaction.options.getSubcommand() == 'api_info') { - await interaction.deferReply() - let url = `${base_url}/Core/GetAPISpec` - let headers = { headers: { SESSIONID: localStorage.getItem("AMP_sessionID") } } - try { - let response = await axios.post(url, headers) - console.log(response.data) - await interaction.editReply('Ok !') - } catch (error) { console.log(error), await interaction.editReply("HTTP Error !") } - }*/ else if (interaction.options.getSubcommandGroup() == 'instances') { - if (interaction.options.getSubcommand() == 'manage') { + if (interaction.options.getSubcommand() == 'list') { await interaction.deferReply() - let friendlyName = interaction.options.getString('name') - let url = `${base_url}/ADSModule/GetInstances` - let headers = { SESSIONID: localStorage.getItem("AMP_sessionID") } - try { - let response = await axios.post(url, headers) - response.data.result[0].AvailableInstances.forEach(element => { - if (element.FriendlyName == friendlyName) { return instanceID = element.InstanceID } + + let result = await getInstances(base_url) + if (result.status === 'success') { + let fields = [] + result.instances.forEach(instance => { + fields.push({ + name: instance.FriendlyName, + value: `**ID:** ${instance.InstanceID} + **Running:** ${instance.Running} + **IP:** ${instance.IP} + **Port:** ${instance.Port} + **Module:** ${instance.Module}`, + inline: true + }) }) - } catch (error) { console.log(error), await interaction.editReply("HTTP Error !") } - - if (!instanceID) { await interaction.editReply(`Aucune instance trouv�e au nom de ${friendlyName} !`) } - - url = `${base_url}/ADSModule/ManageInstance` - headers = { SESSIONID: localStorage.getItem("AMP_sessionID"), - InstanceId: instanceID } - try { - let response = await axios.post(url, headers) - console.log(response) - await interaction.editReply('Ok !') - } catch (error) { console.log(error), await interaction.editReply("HTTP Error !") } + let embed = new EmbedBuilder() + .setTitle('Instances') + .setDescription('Liste de toutes les instances disponibles.') + .setColor(interaction.guild.members.me.displayColor) + .setTimestamp() + .setFields(fields) + return await interaction.followUp({ embeds: [embed] }) + } } - else if (interaction.options.getSubcommand() == 'list') { + else if (interaction.options.getSubcommand() == 'manage') { await interaction.deferReply() - let url = `${base_url}/ADSModule/GetInstances` - let headers = { SESSIONID: localStorage.getItem("AMP_sessionID") } - try { - let response = await axios.post(url, headers) - console.log(response.data.result[0].AvailableInstances) - await interaction.editReply('Ok !') - } catch (error) { console.log(error), await interaction.editReply("HTTP Error !") } + + let result = await manageInstance(base_url, interaction.options.getString('name')) + if (result.status === 'success') return await interaction.followUp(`Ok !`) } else if (interaction.options.getSubcommand() == 'restart') { await interaction.deferReply() - let url = `${base_url}/ADSModule/RestartInstance` - let headers = { SESSIONID: localStorage.getItem("AMP_sessionID"), - InstanceName: interaction.options.getString('name') } + + let result = await restartInstance(base_url, interaction.options.getString('name')) + if (result.status === 'success') return await interaction.followUp(`Ok !`) + } + /*else if (interaction.options.getSubcommand() == 'api_info') { + await interaction.deferReply() + let url = `${base_url}/Core/GetAPISpec` + let headers = { headers: { SESSIONID: localStorage.getItem("AMP_sessionID") } } try { let response = await axios.post(url, headers) console.log(response.data) await interaction.editReply('Ok !') } catch (error) { console.log(error), await interaction.editReply("HTTP Error !") } - } + }*/ } } } \ No newline at end of file diff --git a/utilsAMP/getInstances.js b/utilsAMP/getInstances.js new file mode 100644 index 0000000..1911958 --- /dev/null +++ b/utilsAMP/getInstances.js @@ -0,0 +1,17 @@ +const axios = require('axios') +require('dotenv').config() + +module.exports = getInstances = (async (url) => { + return await axios.post(`${url}/ADSModule/GetInstances`, { + SESSIONID: process.env.AMP_SESSIONID + }).then(response => { + console.log(response.data) + console.log(response.data.result[0].AvailableInstances) + + //if (!response.data.success) return { status: 'fail', data: response.data } + return { status: 'success', data: response.data, instances: response.data.result[0].AvailableInstances } + }).catch(error => { + console.error(error) + return { status: 'error', data: error } + }) +}) \ No newline at end of file diff --git a/utilsAMP/manageInstance.js b/utilsAMP/manageInstance.js new file mode 100644 index 0000000..58d799d --- /dev/null +++ b/utilsAMP/manageInstance.js @@ -0,0 +1,34 @@ +const axios = require('axios') +require('dotenv').config() + +module.exports = manageInstance = (async (url, FriendlyName) => { + let result = await axios.post(`${url}/ADSModule/GetInstances`, { + SESSIONID: process.env.AMP_SESSIONID + }).then(response => { + console.log(response.data) + + let instance_id = response.data.result[0].AvailableInstances.forEach(element => { + if (element.FriendlyName == FriendlyName) return element.InstanceID + }) + + //if (!response.data.success) return { status: 'fail', data: response.data } + return { status: 'success', data: { instance_id } } + }).catch(error => { + console.error(error) + return { status: 'error', data: error } + }) + + if (result.status === 'success') return await axios.post(`${url}/ADSModule/ManageInstance`, { + SESSIONID: process.env.AMP_SESSIONID, + InstanceId: result.data.instance_id + }).then(response => { + console.log(response.data) + + //if (!response.data.success) return { status: 'fail', data: response.data } + return { status: 'success', data: response.data } + }).catch(error => { + console.error(error) + return { status: 'error', data: error } + }) + else return result +}) \ No newline at end of file diff --git a/utilsAMP/restartInstance.js b/utilsAMP/restartInstance.js new file mode 100644 index 0000000..cc0a037 --- /dev/null +++ b/utilsAMP/restartInstance.js @@ -0,0 +1,17 @@ +const axios = require('axios') +require('dotenv').config() + +module.exports = restartInstance = (async (url, InstanceName) => { + return await axios.post(`${url}/ADSModule/RestartInstance`, { + SESSIONID: process.env.AMP_SESSIONID, + InstanceName + }).then(response => { + console.log(response.data) + + //if (!response.data.success) return { status: 'fail', data: response.data } + return { status: 'success', data: response.data } + }).catch(error => { + console.error(error) + return { status: 'error', data: error } + }) +}) \ No newline at end of file