wrapping up join command

- API untested
This commit is contained in:
Logan Cusano
2023-06-03 23:00:50 -04:00
parent 5c8414b4d8
commit fa2f28207e
4 changed files with 30 additions and 47 deletions

View File

@@ -4,7 +4,7 @@ const { DebugBuilder } = require("../utilities/debugBuilder");
const { BufferToJson, getMembersInRole, getKeyByArrayValue } = require("../utilities/utils");
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
const { readFileSync } = require('fs');
const { getOnlineNodes, getNodeInfoFromId } = require("../utilities/mysqlHandler");
const { getOnlineNodes, getNodeInfoFromId, updateNodeInfo } = require("../utilities/mysqlHandler");
const path = require('path');
// Global Vars
@@ -65,22 +65,25 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) {
if (!Object.keys(availableClientIds).length > 0) return log.ERROR("All client ID have been used, consider swapping one of the curretly joined bots or adding more Client IDs to the pool.")
selectedClientId = availableClientIds[Object.keys(availableClientIds)[0]];
}
const selectedNode = nodesCurrentlyAvailable[0];
const reqOptions = new requestOptions("/bot/join", "POST", nodesCurrentlyAvailable[0].ip, nodesCurrentlyAvailable[0].port);
const reqOptions = new requestOptions("/bot/join", "POST", selectedNode.ip, selectedNode.port);
sendHttpRequest(reqOptions, JSON.stringify({
"channelId": channelId,
"clientId": selectedClientId.id,
"presetName": presetName
}), (responseObj) => {
log.VERBOSE("Response Object from node ", nodesCurrentlyAvailable[0], responseObj);
}), async (responseObj) => {
log.VERBOSE("Response Object from node ", selectedNode, responseObj);
if (!responseObj || !responseObj.statusCode == 200) return false;
nodesCurrentlyAvailable.push(nodesCurrentlyAvailable[0]);
// Node has connected to discord
selectedNode.connected = true;
const updatedNode = await updateNodeInfo(selectedNode)
log.DEBUG("Updated Node: ", updatedNode);
});
}
exports.joinServerWrapper = joinServerWrapper;
var presetsAvailable = [];
module.exports = {
data: new customSlashCommandBuilder()
.setName('join')

View File

@@ -6,6 +6,7 @@ const {getAllNodes, addNewNode, updateNodeInfo, getNodeInfoFromId, getOnlineNode
const utils = require("../utilities/utils");
const { sendHttpRequest, requestOptions } = require("../utilities/httpRequests.js");
const { nodeObject } = require("../utilities/recordHelper.js");
const { joinServerWrapper } = require("../commands/join");
const refreshInterval = process.env.NODE_MONITOR_REFRESH_INTERVAL ?? 1200000;
@@ -90,6 +91,21 @@ exports.nodeCheckIn = async (req, res) => {
}
/**
* Request the node to join the specified server/channel and listen to the specified resource
*
* @param req.body.clientId The client ID to join discord with (NOT dev portal ID, right click user -> Copy ID)
* @param req.body.channelId The Channel ID to join in Discord
* @param req.body.presetName The preset name to listen to in Discord
*/
exports.requestNodeJoinServer = async (req, res) => {
if (!req.body.clientId || !req.body.channelId || !req.body.presetName) return res.status(400).json("Missing information in request, requires clientId, channelId, presetName");
await joinServerWrapper(req.body.presetName, req.body.channelId, req.body.clientId)
}
/**
* The node monitor service, this will periodically check in on the online nodes to make sure they are still online
*/
exports.nodeMonitorService = class nodeMonitorService {
constructor() {
}

View File

@@ -28,4 +28,8 @@ router.get('/nodeInfo', nodesController.getNodeInfo);
// Client checkin with the server to update information
router.post('/nodeCheckIn', nodesController.nodeCheckIn);
// TODO Need to authenticate this request
// Request a particular client to join a particular channel listening to a particular preset
router.post('/joinServer', nodesController.requestNodeJoinServer);
module.exports = router;

View File

@@ -42,45 +42,5 @@ exports.customSlashCommandBuilder = class customSlashCommandBuilder extends Slas
return this;
}
/*
return new class extends SlashCommandStringOption {
constructor() {
super();
getAllNodes((nodeObjects) => {
this.name = "preset"
this.required = "false"
var presetsAvailable = [];
for (const nodeObject of nodeObjects) {
nodeObject.nearbySystems = BufferToJson(nodeObject.nearbySystems);
log.DEBUG("Node object: ", nodeObject);
for (const presetName in nodeObject.nearbySystems) presetsAvailable.push(nodeObject.nearbySystems[presetName]);
}
log.DEBUG("All Presets available: ", presetsAvailable);
// Remove duplicates
presetsAvailable = [...new Set(presetsAvailable)];
log.DEBUG("DeDuped Presets available: ", presetsAvailable);
var choicesList = []
for (const preset of presetsAvailable){
log.DEBUG("Preset: ", preset);
choicesList.push({
name: preset,
value: preset
})
}
log.DEBUG("Choice List: ", choicesList);
this.choices = JSON.stringify(choicesList);
return this;
});
}
}
*/
}