Files
DRB-CnC/Client/controllers/botController.js
2023-05-20 14:31:43 -04:00

82 lines
3.0 KiB
JavaScript

// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "clientController");
const spawn = require('child_process').spawn;
const { resolve } = require("path");
const { closeProcessWrapper } = require("../utilities/utilities");
// Global vars
let pythonProcess;
/**
* Get Status of the discord process
*/
exports.getStatus = (req, res) => {
log.INFO("Getting the status of the bot");
if (pythonProcess) return res.sendStatus(200);
return res.sendStatus(201);
}
/**
* Start the bot and join the server and preset specified
*/
exports.joinServer = async (req, res) => {
if (!req.body.clientId || !req.body.deviceId || !req.body.channelId) return res.send("500").json({"message": "You must include the client ID (discord token), device ID (ID of the audio device to use), channel ID (The discord ID of the channel to connect to)"});
const deviceId = req.body.deviceId;
const channelId = req.body.channelId;
const clientId = req.body.clientId;
const presetName = req.body.presetName;
const NGThreshold = req.body.NGThreshold ?? 50
// Joining the discord server
log.INFO("Join requested to: ", deviceId, channelId, clientId);
if (process.platform === "win32") {
log.DEBUG("Starting Windows Python");
pythonProcess = await spawn('H:\\Logan\\Projects\\Python-Discord-Audio-Bot\\venv\\Scripts\\python.exe', [resolve(__dirname, "../PDAB/main.py"), deviceId, channelId, clientId, '-n', NGThreshold], { cwd: resolve(__dirname, "../PDAB/").toString() });
//pythonProcess = await spawn('C:\\Python310\\python.exe', [resolve(__dirname, "../PDAB/main.py"), deviceId, channelId, clientId, NGThreshold ]);
}
else {
log.DEBUG("Starting Linux Python");
pythonProcess = await spawn('python3', [resolve(__dirname, "../PDAB/main.py"), deviceId, channelId, clientId, NGThreshold ], { cwd: resolve(__dirname, "../PDAB/") });
}
log.VERBOSE("Python Process: ", pythonProcess);
let fullOutput;
pythonProcess.stdout.setEncoding('utf8');
pythonProcess.stdout.on("data", (data) => {
log.VERBOSE("From Process: ", data);
fullOutput += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
log.VERBOSE(`stderr: ${data}`);
fullOutput += data.toString();
});
pythonProcess.on('close', (code) => {
log.DEBUG(`child process exited with code ${code}`);
log.VERBOSE("Full output from bot: ", fullOutput);
});
pythonProcess.on("error", (code, signal) => {
log.ERROR("Error from the discord bot process: ", code, signal);
});
// Starting the radio application
return res.sendStatus(200);
}
/**
* Leaves the server if it's in one
*/
exports.leaveServer = async (req, res) => {
log.INFO("Leaving the server");
if (!pythonProcess) return res.sendStatus(200)
pythonProcess = await closeProcessWrapper(pythonProcess);
return res.sendStatus(202);
}