From 62c00eec097ebc29c2e10523b96f994e97deb55c Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 6 Apr 2024 01:02:42 -0400 Subject: [PATCH] Implementing disconnection of discord client --- client/discordAudioBot/pdabHandler.mjs | 21 +++++++++++++++++---- client/discordAudioBot/pdabWrappers.mjs | 17 +++++++++-------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/client/discordAudioBot/pdabHandler.mjs b/client/discordAudioBot/pdabHandler.mjs index 42e5d71..6b06f33 100644 --- a/client/discordAudioBot/pdabHandler.mjs +++ b/client/discordAudioBot/pdabHandler.mjs @@ -3,12 +3,17 @@ 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'; 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); + export const initDiscordBotClient = (clientId, callback) => { const port = process.env.PDAB_PORT || 3000; @@ -29,7 +34,7 @@ export const initDiscordBotClient = (clientId, callback) => { server.listen(port, async () => { console.log(`Server is running on port ${port}`); - launchProcess("python", ["./discordAduioBot/pdab/main.py", process.env.AUDIO_DEVICE_ID, clientId, port], false); + 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 }); } @@ -46,11 +51,11 @@ export const connectToChannel = (channelId) => { }; // Function to emit a command to leave a voice channel -export const leaveVoiceChannel = (guildId) => { - return new Promise((res) => { +export const leaveVoiceChannel = async (guildId) => { + return await new Promise((res) => { io.timeout(25000).emit('leave_server', { guild_id: guildId }, (status, clientRemainsOpen) => { console.log("Discord client remains open?", clientRemainsOpen); - res(clientRemainsOpen) + res(clientRemainsOpen[0]) }); }); }; @@ -94,4 +99,12 @@ export const requestDiscordID = () => { res(result[0]); }); }); +}; + + +export const requestDiscordClientClose = () => { + return new Promise((res) => { + io.timeout(25000).emit('request_client_close'); + res(); + }); }; \ No newline at end of file diff --git a/client/discordAudioBot/pdabWrappers.mjs b/client/discordAudioBot/pdabWrappers.mjs index b4a0b6a..52bde89 100644 --- a/client/discordAudioBot/pdabWrappers.mjs +++ b/client/discordAudioBot/pdabWrappers.mjs @@ -1,4 +1,4 @@ -import { connectToChannel, leaveVoiceChannel, checkIfConnectedToVC, initDiscordBotClient, requestDiscordUsername, requestDiscordID } from './pdabHandler.mjs'; +import { connectToChannel, leaveVoiceChannel, checkIfConnectedToVC, initDiscordBotClient, requestDiscordUsername, requestDiscordID, requestDiscordClientClose } from './pdabHandler.mjs'; import { openOP25, closeOP25 } from '../op25Handler/op25Handler.mjs'; let activeDiscordClient = undefined; @@ -42,14 +42,15 @@ export const joinDiscordVC = async (joinData) => { export const leaveDiscordVC = async (guildId) => { console.log("Leave requested"); if (await checkIfConnectedToVC(guildId)) { - await leaveVoiceChannel(guildId, async (clientRemainsOpen) => { - if (!clientRemainsOpen) { - console.log("There are no open VC connections"); - // TODO DELETE comment DEV ONLY await closeOP25(); + const clientRemainsOpen = await leaveVoiceChannel(guildId); + console.log("Client should remain open: ", clientRemainsOpen); + if (!clientRemainsOpen) { + console.log("There are no open VC connections"); + // TODO DELETE comment DEV ONLY await closeOP25(); - // TODO Close the python client - } - }) + // Close the python client + await requestDiscordClientClose(); + } } }