#2 implement debugger

This commit is contained in:
Logan Cusano
2024-06-02 20:10:55 -04:00
parent e54c80a95b
commit f706ac89b4
15 changed files with 190 additions and 94 deletions

View File

@@ -1,4 +1,6 @@
// server.js
import { DebugBuilder } from "../modules/debugger.mjs";
const log = new DebugBuilder("client", "pdabHandler.mjs");
import express from 'express';
import http from 'http';
import { Server } from 'socket.io';
@@ -29,21 +31,21 @@ export const initDiscordBotClient = (clientId, callback, runPDAB = true) => {
export const startPdabSocketServer = () => {
io.on('connection', (socket) => {
console.log('A user connected');
log.INFO('A user connected');
socket.on('disconnect', () => {
console.log('User disconnected');
log.INFO('User disconnected');
});
// Listen for the discord client ready event
socket.on('discord_ready', (message) => {
console.log("Message from local client", message);
log.INFO("Message from local client", message);
botCallback();
});
});
server.listen(port, async () => {
console.log(`Server is running on port ${port}`);
log.INFO(`Server is running on port ${port}`);
});
return
}
@@ -61,7 +63,7 @@ export const closePdabSocketServer = () => {
export const connectToChannel = (channelId) => {
return new Promise((res) => {
io.timeout(25000).emit('join_server', { channelId: channelId }, (status, value) => {
console.log("Status returned from bot:", status, value);
log.INFO("Status returned from bot:", status, value);
res(value[0]);
});
});
@@ -71,7 +73,7 @@ export const connectToChannel = (channelId) => {
export const leaveVoiceChannel = async (guildId) => {
return await new Promise((res) => {
io.timeout(25000).emit('leave_server', { guildId: guildId }, (status, clientRemainsOpen) => {
console.log("Discord client remains open?", clientRemainsOpen);
log.INFO("Discord client remains open?", clientRemainsOpen);
res(clientRemainsOpen[0])
});
});
@@ -88,13 +90,13 @@ export const setDiscordClientPrsense = (system) => {
// Placeholder functions (replace with actual implementation)
export const checkIfConnectedToVC = async (guildId) => {
console.log("Pdab process var:", pdabProcess);
log.INFO("Pdab process var:", pdabProcess);
if (!pdabProcess) return false;
return await new Promise((res) => {
io.timeout(25000).emit('check_discord_vc_connected', { guildId: guildId }, (status, result) => {
console.log(`Discord VC connected for guild ${guildId}: ${result}`);
log.INFO(`Discord VC connected for guild ${guildId}: ${result}`);
res((result[0]));
});
})
@@ -103,7 +105,7 @@ export const checkIfConnectedToVC = async (guildId) => {
export const requestDiscordUsername = (guildId) => {
return new Promise((res) => {
io.timeout(25000).emit('request_discord_username', { guildId: guildId }, (status, result) => {
console.log(`Discord username: ${result[0]}`);
log.INFO(`Discord username: ${result[0]}`);
res(result[0]);
});
})
@@ -112,7 +114,7 @@ export const requestDiscordUsername = (guildId) => {
export const checkIfClientIsOpen = () => {
return new Promise((res) => {
io.timeout(25000).emit('check_client_is_open', (status, result) => {
console.log(`Client is open: ${result}`);
log.INFO(`Client is open: ${result}`);
res(result[0])
});
});
@@ -121,7 +123,7 @@ export const checkIfClientIsOpen = () => {
export const requestDiscordID = () => {
return new Promise((res) => {
io.timeout(25000).emit('request_discord_id', (status, result) => {
console.log(`Discord ID: ${result}`);
log.INFO(`Discord ID: ${result}`);
res(result[0]);
});
});

View File

@@ -1,3 +1,5 @@
import { DebugBuilder } from "../modules/debugger.mjs";
const log = new DebugBuilder("client", "pdabWrappers");
import { connectToChannel, leaveVoiceChannel, checkIfConnectedToVC, initDiscordBotClient, requestDiscordUsername, requestDiscordID, requestDiscordClientClose, closePdabSocketServer, setDiscordClientPrsense, startPdabSocketServer } from './pdabHandler.mjs';
import { openOP25, closeOP25 } from '../op25Handler/op25Handler.mjs';
@@ -8,38 +10,38 @@ let activeDiscordClient = undefined;
* @param {object} joinData The object containing all the information to join the server
*/
export const joinDiscordVC = async (joinData) => {
console.log("Join requested: ", joinData);
log.INFO("Join requested: ", joinData);
const connection = await new Promise(async (res) => {
// Check if a client already exists
console.log("Checking if there is a client open");
log.INFO("Checking if there is a client open");
if (!await checkIfClientIsOpen()) {
console.log("There is no open client, starting it now");
log.INFO("There is no open client, starting it now");
await startPdabSocketServer();
// Open an instance of OP25
console.log("Starting OP25")
log.INFO("Starting OP25")
openOP25(joinData.system);
// Open a new client and join the requested channel with the requested ID
initDiscordBotClient(joinData.clientID, () => {
console.log("Started PDAB");
log.INFO("Started PDAB");
console.log("Setting the presense of the bot");
log.INFO("Setting the presense of the bot");
setDiscordClientPrsense(joinData.system);
// Add the client object to the IO instance
console.log("Connecting to channel")
log.INFO("Connecting to channel")
connectToChannel(joinData.channelID, (connectionStatus) => {
console.log("Bot Connected to VC:", connectionStatus);
log.INFO("Bot Connected to VC:", connectionStatus);
res(connectionStatus);
});
});
} else {
// Join the requested channel with the requested ID
console.log("There is an open client");
log.INFO("There is an open client");
console.log("Connecting to channel")
log.INFO("Connecting to channel")
const connection = connectToChannel(joinData.channelID);
console.log("Bot Connected to VC::");
log.INFO("Bot Connected to VC::");
res(connection);
}
});
@@ -52,12 +54,12 @@ export const joinDiscordVC = async (joinData) => {
* @param {string} guildId The guild ID to disconnect from VC
*/
export const leaveDiscordVC = async (guildId) => {
console.log("Leave requested");
log.INFO("Leave requested");
if (await checkIfConnectedToVC(guildId)) {
const clientRemainsOpen = await leaveVoiceChannel(guildId);
console.log("Client should remain open: ", clientRemainsOpen);
log.INFO("Client should remain open: ", clientRemainsOpen);
if (!clientRemainsOpen) {
console.log("There are no open VC connections");
log.INFO("There are no open VC connections");
await closeOP25();
// Close the python client
@@ -75,9 +77,9 @@ export const leaveDiscordVC = async (guildId) => {
* @returns {boolean} If the node is connected to VC in the given guild
*/
export const checkIfDiscordVCConnected = async (guildId) => {
console.log("Requested status check");
log.INFO("Requested status check");
if (await checkIfConnectedToVC(guildId)) {
console.log("There is an open VC connection");
log.INFO("There is an open VC connection");
return (true);
} else {
return (false);
@@ -91,7 +93,7 @@ export const checkIfDiscordVCConnected = async (guildId) => {
* @returns {string} The username of the bot in the given guild's VC
*/
export const getDiscordUsername = async (guildId) => {
console.log("Requested username");
log.INFO("Requested username");
if (checkIfClientIsOpen()) {
return await requestDiscordUsername(guildId)
} else return (undefined);
@@ -102,7 +104,7 @@ export const getDiscordUsername = async (guildId) => {
* @returns {string} The ID of the active client
*/
export const getDiscordID = async () => {
console.log("Requested ID");
log.INFO("Requested ID");
if (checkIfClientIsOpen()) {
return await requestDiscordID();
}