From fc113247142a7bd995d2b420b60a4ac0b6173ebf Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 4 Jun 2023 00:24:50 -0400 Subject: [PATCH] Add function to get all client IDs from JSON file #7 --- Server/commands/join.js | 8 +++----- Server/utilities/utils.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Server/commands/join.js b/Server/commands/join.js index 12049c8..b1a4841 100644 --- a/Server/commands/join.js +++ b/Server/commands/join.js @@ -1,11 +1,9 @@ // Modules const { customSlashCommandBuilder } = require('../utilities/customSlashCommandBuilder'); const { DebugBuilder } = require("../utilities/debugBuilder"); -const { BufferToJson, getMembersInRole, getKeyByArrayValue } = require("../utilities/utils"); +const { getMembersInRole, getAllClientIds } = require("../utilities/utils"); const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests"); -const { readFileSync } = require('fs'); -const { getOnlineNodes, getNodeInfoFromId, updateNodeInfo } = require("../utilities/mysqlHandler"); -const path = require('path'); +const { getOnlineNodes, updateNodeInfo } = require("../utilities/mysqlHandler"); // Global Vars const log = new DebugBuilder("server", "join"); @@ -44,7 +42,7 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) { if (!nodesCurrentlyAvailable.length > 0) return Error("All nodes with this channel are unavailable, consider swapping one of the currently joined bots."); // If so, join with the first node - var availableClientIds = await Object(JSON.parse(readFileSync(path.resolve(__dirname, '../clientIds.json')))); + var availableClientIds = await getAllClientIds(); log.DEBUG("All clients: ", Object.keys(availableClientIds)); var selectedClientId; diff --git a/Server/utilities/utils.js b/Server/utilities/utils.js index 8495261..e0d71bd 100644 --- a/Server/utilities/utils.js +++ b/Server/utilities/utils.js @@ -1,6 +1,8 @@ // Debug const { DebugBuilder } = require("../utilities/debugBuilder"); +const { readFileSync } = require('fs'); const log = new DebugBuilder("server", "utils"); +const path = require('path'); // Convert a JSON object to a buffer for the DB exports.JsonToBuffer = (jsonObject) => { @@ -72,4 +74,13 @@ exports.isJsonString = (str) => { return false; } return true; +} + +/** + * Get all client IDs from the saved JSON file + * + * @returns Object of Client IDs + */ +exports.getAllClientIds = () => { + return Object(JSON.parse(readFileSync(path.resolve(__dirname, '../clientIds.json')))); } \ No newline at end of file