Inital move (minus WIP tests)

This commit is contained in:
Logan Cusano
2024-05-12 12:55:00 -04:00
parent 132d974b89
commit 580513997d
21 changed files with 4687 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
// server.js
import express from 'express';
import http from 'http';
import { Server } from 'socket.io';
import { launchProcess } from '../modules/subprocessHandler.mjs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
dotenv.config()
const app = express();
const server = http.createServer(app);
const io = new Server(server);
let pdabProcess = false;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
let botCallback;
export const initDiscordBotClient = (clientId, callback, runPDAB = true) => {
botCallback = callback;
if (runPDAB) launchProcess("python", [join(__dirname, "./pdab/main.py"), process.env.AUDIO_DEVICE_ID, clientId, port], false, join(__dirname, "./pdab"));
pdabProcess = true; // TODO - Make this more dynamic
}
export const startPdabSocketServer = () => {
const port = process.env.PDAB_PORT || 3000;
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('disconnect', () => {
console.log('User disconnected');
});
// Listen for the discord client ready event
socket.on('discord_ready', (message) => {
console.log("Message from local client", message);
botCallback();
});
});
server.listen(port, async () => {
console.log(`Server is running on port ${port}`);
});
return
}
export const closePdabSocketServer = () => {
if (io.sockets && io.sockets.length > 0) {
io.sockets.forEach(socket => {
socket.destroy();
})
}
return io.close();
}
// Function to emit a command to join a voice channel
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);
res(value[0]);
});
});
};
// Function to emit a command to leave a voice channel
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);
res(clientRemainsOpen[0])
});
});
};
// Set the presense of the discord client
export const setDiscordClientPrsense = (system) => {
return new Promise((res) => {
io.timeout(25000).emit('set_system', { system: system }, (status) => {
res();
});
});
};
// Placeholder functions (replace with actual implementation)
export const checkIfConnectedToVC = async (guildId) => {
console.log("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}`);
res((result[0]));
});
})
};
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]}`);
res(result[0]);
});
})
};
export const checkIfClientIsOpen = () => {
return new Promise((res) => {
io.timeout(25000).emit('check_client_is_open', (status, result) => {
console.log(`Client is open: ${result}`);
res(result[0])
});
});
};
export const requestDiscordID = () => {
return new Promise((res) => {
io.timeout(25000).emit('request_discord_id', (status, result) => {
console.log(`Discord ID: ${result}`);
res(result[0]);
});
});
};
export const requestDiscordClientClose = () => {
return new Promise((res) => {
io.timeout(25000).emit('request_client_close');
pdabProcess = false;
res();
});
};

View File

@@ -0,0 +1,122 @@
import { connectToChannel, leaveVoiceChannel, checkIfConnectedToVC, initDiscordBotClient, requestDiscordUsername, requestDiscordID, requestDiscordClientClose, closePdabSocketServer, setDiscordClientPrsense, startPdabSocketServer } from './pdabHandler.mjs';
import { openOP25, closeOP25 } from '../op25Handler/op25Handler.mjs';
let activeDiscordClient = undefined;
/**
* 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 joinDiscordVC = async (joinData) => {
console.log("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");
if (!await checkIfClientIsOpen()) {
console.log("There is no open client, starting it now");
await startPdabSocketServer();
// Open an instance of OP25
console.log("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");
console.log("Setting the presense of the bot");
setDiscordClientPrsense(joinData.system);
// Add the client object to the IO instance
console.log("Connecting to channel")
connectToChannel(joinData.channelID, (connectionStatus) => {
console.log("Bot Connected to VC:", connectionStatus);
res(connectionStatus);
});
});
} else {
// Join the requested channel with the requested ID
console.log("There is an open client");
console.log("Connecting to channel")
const connection = connectToChannel(joinData.channelID);
console.log("Bot Connected to VC::");
res(connection);
}
});
return connection;
}
/**
* Leave VC on the requested server
* @param {string} guildId The guild ID to disconnect from VC
*/
export const leaveDiscordVC = async (guildId) => {
console.log("Leave requested");
if (await checkIfConnectedToVC(guildId)) {
const clientRemainsOpen = await leaveVoiceChannel(guildId);
console.log("Client should remain open: ", clientRemainsOpen);
if (!clientRemainsOpen) {
console.log("There are no open VC connections");
await closeOP25();
// Close the python client
await requestDiscordClientClose();
// Close the IPC server
await closePdabSocketServer();
}
}
}
/**
* 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
* @returns {boolean} If the node is connected to VC in the given guild
*/
export const checkIfDiscordVCConnected = async (guildId) => {
console.log("Requested status check");
if (await checkIfConnectedToVC(guildId)) {
console.log("There is an open VC connection");
return (true);
} else {
return (false);
}
}
/**
* 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
* @returns {string} The username of the bot in the given guild's VC
*/
export const getDiscordUsername = async (guildId) => {
console.log("Requested username");
if (checkIfClientIsOpen()) {
return await requestDiscordUsername(guildId)
} else return (undefined);
}
/**
* Get the ID of the currently running bot
* @returns {string} The ID of the active client
*/
export const getDiscordID = async () => {
console.log("Requested ID");
if (checkIfClientIsOpen()) {
return await requestDiscordID();
}
else return (undefined);
}
/**
* Check if there is an open discord client
* @returns {boolean} If the client is open or not
*/
export const checkIfClientIsOpen = async () => {
if (activeDiscordClient) {
return (true);
}
return (false);
}