From 0f003f907eb35099f59d5e0d33bcdc3a8f7e3a14 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 20 May 2023 00:01:12 -0400 Subject: [PATCH] Discord voice bot handler working --- .gitignore | 1 + Client/controllers/botController.js | 67 +++++++++++++++++++++++++++-- Client/routes/bot.js | 2 +- Client/setup.sh | 7 ++- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 3983a06..83af53e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ node_modules/ *.log *.txt *.env +!requirements.txt \ No newline at end of file diff --git a/Client/controllers/botController.js b/Client/controllers/botController.js index 0940a26..7050977 100644 --- a/Client/controllers/botController.js +++ b/Client/controllers/botController.js @@ -2,23 +2,84 @@ const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "clientController"); +const spawn = require('child_process').spawn; +const { resolve } = require("path"); + +// 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 = (req, res) => { - log.INFO("Joining the server"); +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 = (req, res) => { +exports.leaveServer = async (req, res) => { log.INFO("Leaving the server"); + if (!pythonProcess) return res.sendStatus(200) + + await pythonProcess.kill(2); + await setTimeout(async () => { + if (pythonProcess) await pythonProcess.kill(9); + }, 25000) + pythonProcess = undefined; + + return res.sendStatus(202); } \ No newline at end of file diff --git a/Client/routes/bot.js b/Client/routes/bot.js index 858ebdd..4a0d6ad 100644 --- a/Client/routes/bot.js +++ b/Client/routes/bot.js @@ -5,7 +5,7 @@ const botController = require("../controllers/botController"); /** GET bot status * Check to see if the bot is online and if so, if it is currently connected to anything * - * The status of the bot: 200 = client is online but not connected to discord, 201 = online on discord, 202 = connected to a channel, 500 + JSON = encountered error + * The status of the bot: 200 = connected to discord, 201 = not connected to discord, 500 + JSON = encountered error * @returns status */ router.get('/status', botController.getStatus); diff --git a/Client/setup.sh b/Client/setup.sh index 690b2b0..fede9a7 100644 --- a/Client/setup.sh +++ b/Client/setup.sh @@ -17,10 +17,13 @@ apt-get update apt-get upgrade -y # Install the necessary packages -apt-get install -y nodejs npm libopus-dev gcc make alsa-utils libasound2 libasound2-dev libpulse-dev pulseaudio apulse +apt-get install -y nodejs npm libopus-dev gcc make alsa-utils libasound2 libasound2-dev libpulse-dev pulseaudio apulse python3.9 # Ensure pulse audio is running pulseaudio # Install the node packages from the project -npm i \ No newline at end of file +npm i + +# Install the python packages needed for the bot +pip install -r \ No newline at end of file