// 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, 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(); botChildProcess = undefined; 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; }