Files
bot_Tamiseur/commands/global/amp.js
2023-05-28 02:55:30 +02:00

96 lines
5.1 KiB
JavaScript
Raw Blame History

const { SlashCommandBuilder } = require('discord.js')
require('dotenv').config()
require('require-all')(__dirname + '/../../utilsAMP')
module.exports = {
data: new SlashCommandBuilder().setName('amp').setDescription('Accède à mon panel de jeu AMP !')
//.addSubcommand(subcommand => subcommand.setName('api_info').setDescription('Prints info about the API.'))
.addSubcommand(subcommand => subcommand.setName('login').setDescription("Connectez-vous avant d'effectuer une autre commande !")
.addStringOption(option => option.setName('username').setDescription("Nom d'Utilisateur").setRequired(true))
.addStringOption(option => option.setName('password').setDescription('Mot de Passe').setRequired(true))
.addBooleanOption(option => option.setName('remember').setDescription('Mémoriser les identifiants').setRequired(true))
.addStringOption(option => option.setName('otp').setDescription('Code de double authentification')))
.addSubcommandGroup(subcommandgroup => subcommandgroup.setName('instances').setDescription('Intéragir avec les instances AMP.')
.addSubcommand(subcommand => subcommand.setName('manage').setDescription('Gérer une instance.')
.addStringOption(option => option.setName('name').setDescription("Nom de l'instance").setRequired(true)))
.addSubcommand(subcommand => subcommand.setName('list').setDescription('Liste toutes les instances disponibles.'))
.addSubcommand(subcommand => subcommand.setName('restart').setDescription('Redémarre une instance.')
.addStringOption(option => option.setName('name').setDescription("Nom de l'instance").setRequired(true)))),
async execute(interaction) {
const base_url = process.env.AMP_HOST + '/API'
if (interaction.options.getSubcommand() == 'login') {
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 (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') {
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 }
})
} catch (error) { console.log(error), await interaction.editReply("HTTP Error !") }
if (!instanceID) { await interaction.editReply(`Aucune instance trouv<75>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 !") }
}
else if (interaction.options.getSubcommand() == 'list') {
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 !") }
}
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') }
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 !") }
}
}
}
}