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

@@ -0,0 +1,58 @@
// Config
const { getDeviceID } = require('../utilities/configHandler.js');
// Modules
const alsaInstance = require('alsa-capture');
const { returnAlsaDeviceObject } = require("../utilities/executeConsoleCommands.js");
// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
// Global Vars
const log = new DebugBuilder("client-bot", "audioController");
/**
* Checks to make sure the selected audio device is available and returns the device object (PortAudio Device Info)
* At least one option must be supplied, it will prefer ID to device name
*
* @param deviceName The name of the device being queried
* @param deviceId The ID of the device being queried
* @returns {unknown}
*/
exports.confirmAudioDevice = async function confirmAudioDevice({deviceName = undefined, deviceId = undefined}){
const deviceList = await getAudioDevices();
if (!deviceName && !deviceId) throw new Error("No device given");
if (deviceId) return deviceList.find(device => device.id === deviceId);
if (deviceName) return deviceList.find(device => device.name === deviceName);
}
/**
* Return a list of the audio devices connected with input channels
*
* @returns {unknown[]}
*/
exports.getAudioDevices = async function getAudioDevices(){
// Exec output contains both stderr and stdout outputs
const deviceList = await returnAlsaDeviceObject();
log.DEBUG("Device list: ", deviceList);
return deviceList;
}
/**
* Create and return the audio instance from the saved settings
* TODO Allow the client to save and load these settings dynamically
*
* @returns new portAudio.AudioIO
*/
exports.createAudioInstance = async function createAudioInstance() {
const selectedDevice = await confirmAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"});
log.DEBUG("Device selected from config: ", selectedDevice);
// Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream
return new alsaInstance({
channels: 2,
format: "S16_BE",
rate: 48000,
device: selectedDevice.name ?? "default", // Use -1 or omit the deviceId to select the default device
periodSize: 100, //(48000 / 1000) * 20, //(48000 * 16 * 2) / 1000 * 20 // (48000 * (16 / 8) * 2) / 60 / 1000 * 20 //0.025 * 48000 / 2
periodTime: undefined,
// highwaterMark: 3840
});
}

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