// Modules const { SlashCommandBuilder } = require('discord.js'); const { DebugBuilder } = require("../utilities/debugBuilder"); const { getAllClientIds, getKeyByArrayValue, filterAutocompleteValues } = require("../utilities/utils"); const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests"); const { checkNodeConnectionByClientId, removeNodeConnectionByNodeId, getAllConnections } = require('../utilities/mysqlHandler'); // Global Vars const log = new DebugBuilder("server", "leave"); /** * * @param {*} clientIdObject The client ID object for the node to leave the server. Either 'clientId'||'name' can be set. * @returns */ async function leaveServerWrapper(clientIdObject) { if (!clientIdObject.clientId || !clientIdObject.name) return log.ERROR("Tried to leave server without client ID and/or Name"); const node = await checkNodeConnectionByClientId(clientIdObject); reqOptions = new requestOptions("/bot/leave", "POST", node.ip, node.port); const responseObj = await new Promise((recordResolve, recordReject) => { sendHttpRequest(reqOptions, JSON.stringify({}), async (responseObj) => { recordResolve(responseObj); }); }); log.VERBOSE("Response Object from node ", node, responseObj); if (!responseObj || !responseObj.statusCode == 202) return false; // Node has disconnected from discor // Removing the node connection from the DB const removedConnection = removeNodeConnectionByNodeId(node.id); log.DEBUG("Removed Node Connection: ", removedConnection); return; } exports.leaveServerWrapper = leaveServerWrapper; module.exports = { data: new SlashCommandBuilder() .setName('leave') .setDescription('Disconnect a bot from the server') .addStringOption(option => option.setName("bot") .setDescription("The bot to disconnect from the server") .setAutocomplete(true) .setRequired(true)), example: "leave", isPrivileged: false, requiresTokens: false, defaultTokenUsage: 0, deferInitialReply: true, async autocomplete(interaction) { const connections = await getAllConnections(); const options = connections.map(conn => conn.clientObject.name); await filterAutocompleteValues(interaction, options); }, async execute(interaction) { try{ const botName = interaction.options.getString('bot'); log.DEBUG("Bot Name: ", botName) const clinetIds = await getAllClientIds(); log.DEBUG("Client names: ", clinetIds); const clientDiscordId = getKeyByArrayValue(clinetIds, {'name': botName}); log.DEBUG("Selected bot: ", clinetIds[clientDiscordId]); // Need to create a table in DB to keep track of what bots have what IDs or an endpoint on the clients to return what ID they are running with await leaveServerWrapper(clinetIds[clientDiscordId]); await interaction.editReply(`**${clinetIds[clientDiscordId].name}** has been disconnected`); // This will reply to the initial interaction //await interaction.channel.send('**word.**'); // This will send a message to the channel of the interaction outside of the initial reply }catch(err){ log.ERROR(err) //await interaction.reply(err.toString()); } } }