Allow the client to interact with the discord bot

- The client can now interact with the bot, this allows the client to offer bot controls via API
- The master server will now be able to instruct a specific client to join a specific channel via channel ID (master server function coming soon)
- Suppress some debug statements to make the output easier to read when mixed in with the client debug output
This commit is contained in:
Logan Cusano
2022-12-18 20:31:54 -05:00
parent 6756a5e632
commit c8c75e8b37
9 changed files with 236 additions and 34 deletions

View File

@@ -0,0 +1,86 @@
// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "clientController");
// Modules
const path = require('path');
const fork = require('child_process').fork;
const discordBotPath = path.resolve('discord-bot/app.js');
let botChildProcess, radioChildProcess, tempRes;
/**
* Bot Process Object Builder
*
* This construnctor is used to easily pass commands to the bot process
*/
class BPOB {
/**
* Build an object to be passed to the bot process
* @param command The command to be run ("Status", "Join", "Leave", "ChgPreSet")
* @param parameters Depending on the command being run, there parameters required in order to be run
*/
constructor(command = "Status"||"Join"||"Leave"||"ChgPreSet", parameters = []||undefined) {
this.cmd = command;
if (parameters) this.params = parameters;
}
}
/**
* Get Status of the discord process
*/
exports.getStatus = (req, res) => {
if (!botChildProcess) return res.sendStatus(200);
botChildProcess.send(new BPOB("Status"));
tempRes = res;
}
/**
* Start the bot and join the server and preset specified
*/
exports.joinServer = (req, res) => {
const channelID = req.body.channelID;
const presetName = req.body.presetName;
if (!channelID || !presetName) return res.status(400).json({'message': "Channel ID or Preset Name not present in the request"});
// Start the bot
botChildProcess = fork(discordBotPath);
// Handle bot responses
botChildProcess.on('message', (msg) => {
log.DEBUG('Child response: ', msg);
if (msg.msg === "INIT READY") {
// Discord bot has started and is ready.
botChildProcess.send(new BPOB("Join", {"channelID": channelID, "presetName": presetName}))
tempRes = res;
}
switch (msg.cmd){
case "Status":
if (msg.msg === "VDISCONN") tempRes.sendStatus(201); // VDISCONN == Voice DISCONNected
else tempRes.sendStatus(202);
tempRes = undefined;
return;
case "Join":
tempRes.sendStatus(202);
tempRes = undefined;
return;
case "Leave":
tempRes.sendStatus(202);
tempRes = undefined;
botChildProcess.kill();
return;
case "ChgPreSet":
tempRes.sendStatus(200);
tempRes = undefined;
return;
}
})
}
/**
* Leaves the server if it's in one
*/
exports.leaveServer = (req, res) => {
if (!botChildProcess) return res.sendStatus(200)
botChildProcess.send(new BPOB("Leave"));
tempRes = res;
}