- #7 needs to error check more - both need to be cleaned up
This commit is contained in:
@@ -3,7 +3,7 @@ const { customSlashCommandBuilder } = require('../utilities/customSlashCommandBu
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const { getMembersInRole, getAllClientIds } = require("../utilities/utils");
|
||||
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
|
||||
const { getOnlineNodes, updateNodeInfo, addNodeConnection } = require("../utilities/mysqlHandler");
|
||||
const { getOnlineNodes, updateNodeInfo, addNodeConnection, getConnectionByNodeId } = require("../utilities/mysqlHandler");
|
||||
|
||||
// Global Vars
|
||||
const log = new DebugBuilder("server", "join");
|
||||
@@ -22,22 +22,21 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) {
|
||||
getOnlineNodes((nodeRows) => {
|
||||
recordResolve(nodeRows);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Check which nodes have the selected preset
|
||||
onlineNodes = onlineNodes.filter(node => node.nearbySystems.includes(presetName));
|
||||
log.DEBUG("Filtered Online Nodes: ", onlineNodes);
|
||||
|
||||
// Check if any nodes with this preset are available
|
||||
var nodesCurrentlyAvailable = [];
|
||||
for (const node of onlineNodes) {
|
||||
const reqOptions = new requestOptions("/bot/status", "GET", node.ip, node.port);
|
||||
await new Promise(resolve => sendHttpRequest(reqOptions, "", (responseObj) => {
|
||||
if (!responseObj || !responseObj.statusCode == 200) return resolve(false);
|
||||
log.VERBOSE("Response Object from node ", node, responseObj);
|
||||
nodesCurrentlyAvailable.push(node);
|
||||
resolve(true);
|
||||
}));
|
||||
const currentConnection = await getConnectionByNodeId(node.id);
|
||||
log.DEBUG("Checking to see if there is a connection for Node: ", node, currentConnection);
|
||||
if(!currentConnection) nodesCurrentlyAvailable.push(node);
|
||||
}
|
||||
log.DEBUG("Nodes Currently Available: ", nodesCurrentlyAvailable);
|
||||
|
||||
// If not, let the user know
|
||||
if (!nodesCurrentlyAvailable.length > 0) return Error("All nodes with this channel are unavailable, consider swapping one of the currently joined bots.");
|
||||
|
||||
@@ -77,9 +76,7 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) {
|
||||
log.VERBOSE("Response Object from node ", selectedNode, responseObj);
|
||||
if (!responseObj || !responseObj.statusCode == 200) return false;
|
||||
// Node has connected to discord
|
||||
|
||||
// Updating node Object in DB
|
||||
selectedNode.connected = true;
|
||||
const updatedNode = await updateNodeInfo(selectedNode);
|
||||
log.DEBUG("Updated Node: ", updatedNode);
|
||||
|
||||
@@ -104,15 +101,16 @@ module.exports = {
|
||||
try{
|
||||
const guildId = interaction.guild.id;
|
||||
const presetName = interaction.options.getString('preset');
|
||||
if (!interaction.member.voice.channel.id) return interaction.editReply(`You need to be in a voice channel, ${interaction.user}`)
|
||||
const channelId = interaction.member.voice.channel.id;
|
||||
log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`);
|
||||
await interaction.editReply('**Pong.**');
|
||||
log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`);
|
||||
|
||||
const onlineBots = await getMembersInRole(interaction);
|
||||
|
||||
log.DEBUG("Online Bots: ", onlineBots);
|
||||
|
||||
await joinServerWrapper(presetName, channelId, onlineBots.online);
|
||||
await interaction.editReply('**Pong.**');
|
||||
//await interaction.channel.send('**Pong.**'); // This will send a message to the channel of the interaction outside of the initial reply
|
||||
}catch(err){
|
||||
log.ERROR(err)
|
||||
|
||||
@@ -24,13 +24,7 @@ async function leaveServerWrapper(clientIdObject) {
|
||||
|
||||
log.VERBOSE("Response Object from node ", node, responseObj);
|
||||
if (!responseObj || !responseObj.statusCode == 202) return false;
|
||||
// Node has disconnected from discord
|
||||
|
||||
// Updating the node object in the DB
|
||||
node.connected = false;
|
||||
const updatedNode = await updateNodeInfo(node)
|
||||
log.DEBUG("Updated Node: ", updatedNode);
|
||||
|
||||
// Node has disconnected from discor
|
||||
// Removing the node connection from the DB
|
||||
const removedConnection = removeNodeConnectionByNodeId(node.id);
|
||||
log.DEBUG("Removed Node Connection: ", removedConnection);
|
||||
|
||||
Reference in New Issue
Block a user