Working on #9

- Can join and leave from voice channels
- Will check to make sure that the bot is in a given system or no system before joining
- Cleaned up the socket client with wrappers
- Added a new module to handle subprocesses for the client
- Beginning workings on OP25 handler
- Added OP25 config object generator with config exporter
This commit is contained in:
Logan Cusano
2024-03-03 00:10:43 -05:00
parent 63ad20b9b3
commit 35f3f07793
8 changed files with 504 additions and 64 deletions

View File

@@ -1,6 +1,5 @@
import { io } from "socket.io-client";
import { connectToChannel, initDiscordBotClient, getVoiceChannelFromID, checkIfConnectedToVC, getVoiceConnectionFromGuild } from '../discordAudioBot/dab.mjs';
import { logIntoServerWrapper, sendNodeUpdateWrapper, nodeCheckStatus } from "./socketClientWrappers.mjs";
import { logIntoServerWrapper, nodeCheckStatus, nodeJoinServer, nodeLeaveServer, nodeGetUsername, nodeCheckDiscordClientStatus, nodeCheckCurrentSystem } from "./socketClientWrappers.mjs";
/**
* Initialize the socket connection with the server, this will handle disconnects within itself
@@ -12,53 +11,36 @@ export const initSocketConnection = async (localNodeConfig) => {
const socket = io.connect(serverEndpoint);
const discordClients = {};
// Socket Events ('system' events persay)
// When the socket connects to the node server
socket.on('connect', async () => {
console.log('Connected to the server');
await logIntoServerWrapper(socket, localNodeConfig);
});
socket.on('node-join', async (joinData) => {
console.log("Join requested: ", joinData)
// TODO - Implement logic to control OP25 for the requested channel/system
// Join the requested channel with the requested ID
initDiscordBotClient(joinData.clientID, joinData.system, client => {
getVoiceChannelFromID(client, joinData.channelID).then(vc => {
// Add the client object to the IO instance
discordClients[vc.guild.id] = client;
const connection = connectToChannel(vc);
console.log("Bot Connected to VC");
})
});
});
socket.on('node-leave', async (guildId) => {
console.log("Leave requested");
if (await checkIfConnectedToVC(guildId)) {
const connection = await getVoiceConnectionFromGuild(guildId);
if (connection) {
console.log("There is an open VC connection, closing it now");
// Destroy the open VC connection
connection.destroy();
// Remove the client from the socket connection
delete discordClients[guildId];
}
}
});
socket.on('node-get-discord-username', async (guildId, socketCallback) => {
console.log("Requested username");
socketCallback(discordClients[guildId].user.username);
});
socket.on('node-check-connected-status', nodeCheckStatus);
// When the socket disconnects from the node server
socket.on('disconnect', () => {
console.log('Disconnected from the server');
});
// Node events/commands
// Requested to join a discord guild and listen to a system
socket.on('node-join', nodeJoinServer);
// Requested to leave a discord guild
socket.on('node-leave', nodeLeaveServer);
// Requested to get the discord username in a given guild
socket.on('node-get-discord-username', nodeGetUsername);
// Requested to check if the node is connected to VC in a given guild
socket.on('node-check-connected-status', nodeCheckStatus);
// Requested to check if the node has an open discord client
socket.on('node-check-discord-open-client', nodeCheckDiscordClientStatus);
// Requested to get the current listening system
socket.on('node-check-current-system', nodeCheckCurrentSystem);
return socket;
}

View File

@@ -1,4 +1,5 @@
import { checkIfConnectedToVC } from '../discordAudioBot/dab.mjs';
import { checkIfDiscordVCConnected, joinDiscordVC, leaveDiscordVC, getDiscordUsername, checkIfClientIsOpen } from '../discordAudioBot/dabWrappers.mjs';
import { getCurrentSystem } from '../op25Handler/op25Handler.mjs';
/**
* Wrapper to log into the server
@@ -27,19 +28,20 @@ export const sendNodeUpdateWrapper = async (socket, localNodeConfig) => {
}
export const nodeJoinServer = async (joinData) => {
console.log("Join requested: ", joinData)
// TODO - Implement logic to control OP25 for the requested channel/system
/**
* Join the requested server VC and listen to the requested system
* @param {object} joinData The object containing all the information to join the server
*/
export const nodeJoinServer = async (joinData) => {
await joinDiscordVC(joinData);
}
// Join the requested channel with the requested ID
initDiscordBotClient(joinData.clientID, joinData.system, client => {
getVoiceChannelFromID(client, joinData.channelID).then(vc => {
// Add the client object to the IO instance
discordClients[vc.guild.id] = client;
const connection = connectToChannel(vc);
console.log("Bot Connected to VC");
})
});
/**
* Leave VC on the requested server
* @param {string} guildId The guild ID to disconnect from VC
*/
export const nodeLeaveServer = async (guildId) => {
await leaveDiscordVC(guildId);
}
@@ -47,14 +49,37 @@ export const nodeJoinServer = async (joinData) => {
* Check if the bot is connected to a discord VC in the given server
* @param {string} guildId The guild id to check the connection status in
* @param {any} socketCallback The callback function to return the result to
* @returns {any}
* @callback {boolean} If the node is connected to VC in the given guild
*/
export const nodeCheckStatus = async (guildId, socketCallback) => {
console.log("Requested status check");
if (await checkIfConnectedToVC(guildId)) {
console.log("There is an open VC connection");
socketCallback(true);
} else {
socketCallback(false);
}
socketCallback(await checkIfDiscordVCConnected(guildId));
}
/**
* Get the username of the bot in a given guild
* (there may be a server nickname given to the bot in a certain guild)
* @param {string} guildId The guild id to check the connection status in
* @param {any} socketCallback The callback function to return the result to
* @callback {any}
*/
export const nodeGetUsername = async (guildId, socketCallback) => {
socketCallback(await getDiscordUsername(guildId));
}
/**
* Check if the local node has an open discord client in any server
* @callback {boolean} If the node has an open discord client or not
*/
export const nodeCheckDiscordClientStatus = async (socketCallback) => {
socketCallback(await checkIfClientIsOpen());
}
/**
* Check what system the local node is currently listening to
* @callback {boolean} If the node has an open discord client or not
*/
export const nodeCheckCurrentSystem = async (socketCallback) => {
socketCallback(await getCurrentSystem());
}

View File

@@ -0,0 +1,66 @@
import { spawn } from "child_process";
/**
* Object to store references to spawned processes.
* @type {Object.<string, import('child_process').ChildProcess>}
*/
const runningProcesses = {};
/**
* Launches a new process if it's not already running.
* @param {string} processName - The name of the process to launch.
* @param {string[]} args - The arguments to pass to the process.
*/
export const launchProcess = (processName, args) => {
if (!runningProcesses[processName]) {
const childProcess = spawn(processName, args);
// Store reference to the spawned process
runningProcesses[processName] = childProcess;
childProcess.on('exit', (code, signal) => {
// Remove reference to the process when it exits
delete runningProcesses[processName];
console.log(`${processName} process exited with code ${code} and signal ${signal}`);
});
console.log(`${processName} process started.`);
} else {
console.log(`${processName} process is already running.`);
}
}
/**
* Checks the status of a process.
* @param {string} processName - The name of the process to check.
* @returns {string} A message indicating whether the process is running or not.
*/
export const checkProcessStatus = (processName) => {
const childProcess = runningProcesses[processName];
if (childProcess) {
// Check if the process is running
if (!childProcess.killed) {
return `${processName} process is running.`;
} else {
return `${processName} process is not running.`;
}
} else {
return `${processName} process is not running.`;
}
}
/**
* Kills a running process.
* @param {string} processName - The name of the process to kill.
*/
export const killProcess = (processName) => {
const childProcess = runningProcesses[processName];
if (childProcess) {
childProcess.kill();
console.log(`${processName} process killed.`);
} else {
console.log(`${processName} process is not running.`);
}
}
export const getRunningProcesses = () => runningProcesses;