Discord voice bot handler working

This commit is contained in:
Logan Cusano
2023-05-20 00:01:12 -04:00
parent e7b802839e
commit 0f003f907e
4 changed files with 71 additions and 6 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ node_modules/
*.log
*.txt
*.env
!requirements.txt

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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
npm i
# Install the python packages needed for the bot
pip install -r