Bugfixes and functional #7 & #9

- #7 needs to error check more
- both need to be cleaned up
This commit is contained in:
Logan Cusano
2023-06-11 04:40:40 -04:00
parent e8d68b2da7
commit f5e119d845
4 changed files with 18 additions and 32 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -32,8 +32,6 @@ function returnNodeObjectFromRow(row) {
_location: row.location,
_nearbySystems: BufferToJson(row.nearbySystems),
_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}
*/
async function returnConnectionObjectFromRow(row) {
if (Array.isArray(row)) row = row[0]
log.DEBUG("Connection row: ", row);
return new connectionObject({
_connection_id: row.connection_id,
@@ -111,6 +110,8 @@ exports.getOnlineNodes = (callback) => {
* @param callback Callback function
*/
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 sqlResponse = await new Promise((recordResolve, recordReject) => {
@@ -161,8 +162,7 @@ exports.updateNodeInfo = async (nodeObject, callback = undefined) => {
ip = nodeObject.ip,
port = nodeObject.port,
location = nodeObject.location,
online = nodeObject.online,
connected = nodeObject.connected;
online = nodeObject.online
let queryParams = [],
nearbySystems = nodeObject.nearbySystems;
@@ -178,10 +178,6 @@ exports.updateNodeInfo = async (nodeObject, callback = undefined) => {
if (online || online === 1) queryParams.push(`online = 1`);
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`
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);
if (!sqlResponse) return (callback) ? callback(undefined) : undefined;
if (!sqlResponse | sqlResponse.length == 0) return (callback) ? callback(undefined) : undefined;
const newConnectionObject = await returnConnectionObjectFromRow(sqlResponse)
log.DEBUG("Connection Object from SQL Response: ", newConnectionObject);
return (callback) ? callback(newConnectionObject) : newConnectionObject;

View File

@@ -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._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.name = _name;
this.ip = _ip;
@@ -122,8 +122,6 @@ class nodeObject {
this.location = _location;
this.nearbySystems = _nearbySystems;
this.online = _online;
this.connected = _connected;
this.connection = _connection;
}
}