- #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 { DebugBuilder } = require("../utilities/debugBuilder");
|
||||||
const { getMembersInRole, getAllClientIds } = require("../utilities/utils");
|
const { getMembersInRole, getAllClientIds } = require("../utilities/utils");
|
||||||
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
|
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
|
||||||
const { getOnlineNodes, updateNodeInfo, addNodeConnection } = require("../utilities/mysqlHandler");
|
const { getOnlineNodes, updateNodeInfo, addNodeConnection, getConnectionByNodeId } = require("../utilities/mysqlHandler");
|
||||||
|
|
||||||
// Global Vars
|
// Global Vars
|
||||||
const log = new DebugBuilder("server", "join");
|
const log = new DebugBuilder("server", "join");
|
||||||
@@ -23,21 +23,20 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) {
|
|||||||
recordResolve(nodeRows);
|
recordResolve(nodeRows);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check which nodes have the selected preset
|
// Check which nodes have the selected preset
|
||||||
onlineNodes = onlineNodes.filter(node => node.nearbySystems.includes(presetName));
|
onlineNodes = onlineNodes.filter(node => node.nearbySystems.includes(presetName));
|
||||||
log.DEBUG("Filtered Online Nodes: ", onlineNodes);
|
log.DEBUG("Filtered Online Nodes: ", onlineNodes);
|
||||||
|
|
||||||
// Check if any nodes with this preset are available
|
// Check if any nodes with this preset are available
|
||||||
var nodesCurrentlyAvailable = [];
|
var nodesCurrentlyAvailable = [];
|
||||||
for (const node of onlineNodes) {
|
for (const node of onlineNodes) {
|
||||||
const reqOptions = new requestOptions("/bot/status", "GET", node.ip, node.port);
|
const currentConnection = await getConnectionByNodeId(node.id);
|
||||||
await new Promise(resolve => sendHttpRequest(reqOptions, "", (responseObj) => {
|
log.DEBUG("Checking to see if there is a connection for Node: ", node, currentConnection);
|
||||||
if (!responseObj || !responseObj.statusCode == 200) return resolve(false);
|
if(!currentConnection) nodesCurrentlyAvailable.push(node);
|
||||||
log.VERBOSE("Response Object from node ", node, responseObj);
|
|
||||||
nodesCurrentlyAvailable.push(node);
|
|
||||||
resolve(true);
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
log.DEBUG("Nodes Currently Available: ", nodesCurrentlyAvailable);
|
log.DEBUG("Nodes Currently Available: ", nodesCurrentlyAvailable);
|
||||||
|
|
||||||
// If not, let the user know
|
// 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.");
|
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);
|
log.VERBOSE("Response Object from node ", selectedNode, responseObj);
|
||||||
if (!responseObj || !responseObj.statusCode == 200) return false;
|
if (!responseObj || !responseObj.statusCode == 200) return false;
|
||||||
// Node has connected to discord
|
// Node has connected to discord
|
||||||
|
|
||||||
// Updating node Object in DB
|
// Updating node Object in DB
|
||||||
selectedNode.connected = true;
|
|
||||||
const updatedNode = await updateNodeInfo(selectedNode);
|
const updatedNode = await updateNodeInfo(selectedNode);
|
||||||
log.DEBUG("Updated Node: ", updatedNode);
|
log.DEBUG("Updated Node: ", updatedNode);
|
||||||
|
|
||||||
@@ -104,15 +101,16 @@ module.exports = {
|
|||||||
try{
|
try{
|
||||||
const guildId = interaction.guild.id;
|
const guildId = interaction.guild.id;
|
||||||
const presetName = interaction.options.getString('preset');
|
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;
|
const channelId = interaction.member.voice.channel.id;
|
||||||
log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`);
|
log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`);
|
||||||
await interaction.editReply('**Pong.**');
|
|
||||||
|
|
||||||
const onlineBots = await getMembersInRole(interaction);
|
const onlineBots = await getMembersInRole(interaction);
|
||||||
|
|
||||||
log.DEBUG("Online Bots: ", onlineBots);
|
log.DEBUG("Online Bots: ", onlineBots);
|
||||||
|
|
||||||
await joinServerWrapper(presetName, channelId, onlineBots.online);
|
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
|
//await interaction.channel.send('**Pong.**'); // This will send a message to the channel of the interaction outside of the initial reply
|
||||||
}catch(err){
|
}catch(err){
|
||||||
log.ERROR(err)
|
log.ERROR(err)
|
||||||
|
|||||||
@@ -24,13 +24,7 @@ async function leaveServerWrapper(clientIdObject) {
|
|||||||
|
|
||||||
log.VERBOSE("Response Object from node ", node, responseObj);
|
log.VERBOSE("Response Object from node ", node, responseObj);
|
||||||
if (!responseObj || !responseObj.statusCode == 202) return false;
|
if (!responseObj || !responseObj.statusCode == 202) return false;
|
||||||
// Node has disconnected from discord
|
// Node has disconnected from discor
|
||||||
|
|
||||||
// Updating the node object in the DB
|
|
||||||
node.connected = false;
|
|
||||||
const updatedNode = await updateNodeInfo(node)
|
|
||||||
log.DEBUG("Updated Node: ", updatedNode);
|
|
||||||
|
|
||||||
// Removing the node connection from the DB
|
// Removing the node connection from the DB
|
||||||
const removedConnection = removeNodeConnectionByNodeId(node.id);
|
const removedConnection = removeNodeConnectionByNodeId(node.id);
|
||||||
log.DEBUG("Removed Node Connection: ", removedConnection);
|
log.DEBUG("Removed Node Connection: ", removedConnection);
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ function returnNodeObjectFromRow(row) {
|
|||||||
_location: row.location,
|
_location: row.location,
|
||||||
_nearbySystems: BufferToJson(row.nearbySystems),
|
_nearbySystems: BufferToJson(row.nearbySystems),
|
||||||
_online: (row.online === 1) ? true : false,
|
_online: (row.online === 1) ? true : false,
|
||||||
_connected: (row.connected === 1) ? true : false,
|
|
||||||
_connection: (row.connection) ? row.connection : null,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +60,7 @@ function returnNodeObjectFromRows(rows) {
|
|||||||
* @returns {connectionObject}
|
* @returns {connectionObject}
|
||||||
*/
|
*/
|
||||||
async function returnConnectionObjectFromRow(row) {
|
async function returnConnectionObjectFromRow(row) {
|
||||||
|
if (Array.isArray(row)) row = row[0]
|
||||||
log.DEBUG("Connection row: ", row);
|
log.DEBUG("Connection row: ", row);
|
||||||
return new connectionObject({
|
return new connectionObject({
|
||||||
_connection_id: row.connection_id,
|
_connection_id: row.connection_id,
|
||||||
@@ -111,6 +110,8 @@ exports.getOnlineNodes = (callback) => {
|
|||||||
* @param callback Callback function
|
* @param callback Callback function
|
||||||
*/
|
*/
|
||||||
async function getNodeInfoFromId(nodeId, callback = undefined) {
|
async function getNodeInfoFromId(nodeId, callback = undefined) {
|
||||||
|
if (!nodeId) throw new Error("No node ID given when trying to fetch node");
|
||||||
|
log.DEBUG("Getting node from ID: ", nodeId);
|
||||||
const sqlQuery = `SELECT * FROM ${nodesTable} WHERE id = ${nodeId}`
|
const sqlQuery = `SELECT * FROM ${nodesTable} WHERE id = ${nodeId}`
|
||||||
|
|
||||||
const sqlResponse = await new Promise((recordResolve, recordReject) => {
|
const sqlResponse = await new Promise((recordResolve, recordReject) => {
|
||||||
@@ -161,8 +162,7 @@ exports.updateNodeInfo = async (nodeObject, callback = undefined) => {
|
|||||||
ip = nodeObject.ip,
|
ip = nodeObject.ip,
|
||||||
port = nodeObject.port,
|
port = nodeObject.port,
|
||||||
location = nodeObject.location,
|
location = nodeObject.location,
|
||||||
online = nodeObject.online,
|
online = nodeObject.online
|
||||||
connected = nodeObject.connected;
|
|
||||||
let queryParams = [],
|
let queryParams = [],
|
||||||
nearbySystems = nodeObject.nearbySystems;
|
nearbySystems = nodeObject.nearbySystems;
|
||||||
|
|
||||||
@@ -178,10 +178,6 @@ exports.updateNodeInfo = async (nodeObject, callback = undefined) => {
|
|||||||
if (online || online === 1) queryParams.push(`online = 1`);
|
if (online || online === 1) queryParams.push(`online = 1`);
|
||||||
else queryParams.push(`online = 0`);
|
else queryParams.push(`online = 0`);
|
||||||
}
|
}
|
||||||
if (typeof connected === "boolean" || typeof connected === "number") {
|
|
||||||
if (online || online === 1) queryParams.push(`connected = 1`);
|
|
||||||
else queryParams.push(`connected = 0`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let sqlQuery = `UPDATE ${nodesTable} SET`
|
let sqlQuery = `UPDATE ${nodesTable} SET`
|
||||||
if (!queryParams || queryParams.length === 0) return (callback) ? callback(undefined) : undefined;
|
if (!queryParams || queryParams.length === 0) return (callback) ? callback(undefined) : undefined;
|
||||||
@@ -276,7 +272,7 @@ exports.getConnectionByNodeId = async (nodeId, callback = undefined) => {
|
|||||||
|
|
||||||
log.VERBOSE("SQL Response from checking connection: ", sqlResponse);
|
log.VERBOSE("SQL Response from checking connection: ", sqlResponse);
|
||||||
|
|
||||||
if (!sqlResponse) return (callback) ? callback(undefined) : undefined;
|
if (!sqlResponse | sqlResponse.length == 0) return (callback) ? callback(undefined) : undefined;
|
||||||
const newConnectionObject = await returnConnectionObjectFromRow(sqlResponse)
|
const newConnectionObject = await returnConnectionObjectFromRow(sqlResponse)
|
||||||
log.DEBUG("Connection Object from SQL Response: ", newConnectionObject);
|
log.DEBUG("Connection Object from SQL Response: ", newConnectionObject);
|
||||||
return (callback) ? callback(newConnectionObject) : newConnectionObject;
|
return (callback) ? callback(newConnectionObject) : newConnectionObject;
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ class nodeObject {
|
|||||||
* @param {*} param0._connection The connection Object associated with the node, null if not checked, undefined if none exists
|
* @param {*} param0._connection The connection Object associated with the node, null if not checked, undefined if none exists
|
||||||
* @param {*} param0._nearbySystems An object array of nearby systems
|
* @param {*} param0._nearbySystems An object array of nearby systems
|
||||||
*/
|
*/
|
||||||
constructor({ _id = null, _name = null, _ip = null, _port = null, _location = null, _nearbySystems = null, _online = null, _connected = null, _connection = null }) {
|
constructor({ _id = null, _name = null, _ip = null, _port = null, _location = null, _nearbySystems = null, _online = null }) {
|
||||||
this.id = _id;
|
this.id = _id;
|
||||||
this.name = _name;
|
this.name = _name;
|
||||||
this.ip = _ip;
|
this.ip = _ip;
|
||||||
@@ -122,8 +122,6 @@ class nodeObject {
|
|||||||
this.location = _location;
|
this.location = _location;
|
||||||
this.nearbySystems = _nearbySystems;
|
this.nearbySystems = _nearbySystems;
|
||||||
this.online = _online;
|
this.online = _online;
|
||||||
this.connected = _connected;
|
|
||||||
this.connection = _connection;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user