78 lines
3.3 KiB
JavaScript
78 lines
3.3 KiB
JavaScript
// Modules
|
|
const { customSlashCommandBuilder } = require('../utilities/customSlashCommandBuilder');
|
|
const { DebugBuilder } = require("../utilities/debugBuilder");
|
|
const { getAllClientIds, getKeyByArrayValue } = require("../utilities/utils");
|
|
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
|
|
const { checkNodeConnectionByClientId, removeNodeConnectionByNodeId, updateNodeInfo, getConnectedNodes, getAllConnections } = require('../utilities/mysqlHandler');
|
|
|
|
// Global Vars
|
|
const log = new DebugBuilder("server", "leave");
|
|
const logAC = new DebugBuilder("server", "leave_autocorrect");
|
|
|
|
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 customSlashCommandBuilder()
|
|
.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 focusedValue = interaction.options.getFocused();
|
|
const connections = await getAllConnections();
|
|
const filtered = connections.filter(conn => String(conn.clientObject.name).startsWith(focusedValue)).map(conn => conn.clientObject.name);
|
|
logAC.DEBUG("Focused Value: ", focusedValue, connections, filtered);
|
|
await interaction.respond(
|
|
filtered.map(option => ({ name: option, value: option })),
|
|
);
|
|
},
|
|
async execute(interaction) {
|
|
try{
|
|
const guildId = interaction.guild.id;
|
|
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());
|
|
}
|
|
}
|
|
} |