Initial update to try and fix #1

This commit is contained in:
Logan Cusano
2023-03-26 15:03:35 -04:00
parent 403b533a33
commit b296da629b
14 changed files with 225 additions and 120 deletions

View File

@@ -5,33 +5,29 @@ const log = new DebugBuilder("client", "clientController");
const path = require('path');
const fork = require('child_process').fork;
const discordBotPath = path.resolve('discord-bot/app.js');
let botChildProcess, tempRes;
// Commands
const ping = require('../commands/ping.js');
const join = require('../commands/join.js');
const leave = require('../commands/leave.js');
const status = require('../commands/status.js');
/**
* Bot Process Object Builder
*
* This construnctor is used to easily pass commands to the bot process
* Get an object of client guilds
* @returns
*/
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;
}
function getGuilds() {
return client.guilds.cache.map(guild => guild.id)
}
/**
* Get Status of the discord process
*/
exports.getStatus = (req, res) => {
if (!botChildProcess) return res.sendStatus(200);
botChildProcess.send(new BPOB("Status"));
tempRes = res;
status({guildID: guildID, callback: (statusObj) => {
log.DEBUG("Status Object string: ", statusObj);
if (!statusObj.voiceConnection) return req.sendStatus(201);
return req.sendStatus(202);
}});
}
/**
@@ -43,45 +39,16 @@ exports.joinServer = (req, res) => {
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;
}
})
join({guildID: guildID, guildObj: client.guilds.cache.get(guildID), channelID: channelID, callback: () => {
return req.sendStatus(202);
}});
}
/**
* 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;
leave({guildID: guildID, callback: (response) => {
return req.sendStatus(202);
}});
}