Implementing disconnection of discord client

This commit is contained in:
Logan Cusano
2024-04-06 01:02:42 -04:00
parent ea63abcb93
commit 62c00eec09
2 changed files with 26 additions and 12 deletions

View File

@@ -3,12 +3,17 @@ import express from 'express';
import http from 'http'; import http from 'http';
import { Server } from 'socket.io'; import { Server } from 'socket.io';
import { launchProcess } from '../modules/subprocessHandler.mjs'; import { launchProcess } from '../modules/subprocessHandler.mjs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
const io = new Server(server); const io = new Server(server);
let pdabProcess = false; let pdabProcess = false;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const initDiscordBotClient = (clientId, callback) => { export const initDiscordBotClient = (clientId, callback) => {
const port = process.env.PDAB_PORT || 3000; const port = process.env.PDAB_PORT || 3000;
@@ -29,7 +34,7 @@ export const initDiscordBotClient = (clientId, callback) => {
server.listen(port, async () => { server.listen(port, async () => {
console.log(`Server is running on port ${port}`); 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 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 // Function to emit a command to leave a voice channel
export const leaveVoiceChannel = (guildId) => { export const leaveVoiceChannel = async (guildId) => {
return new Promise((res) => { return await new Promise((res) => {
io.timeout(25000).emit('leave_server', { guild_id: guildId }, (status, clientRemainsOpen) => { io.timeout(25000).emit('leave_server', { guild_id: guildId }, (status, clientRemainsOpen) => {
console.log("Discord client remains open?", clientRemainsOpen); console.log("Discord client remains open?", clientRemainsOpen);
res(clientRemainsOpen) res(clientRemainsOpen[0])
}); });
}); });
}; };
@@ -94,4 +99,12 @@ export const requestDiscordID = () => {
res(result[0]); res(result[0]);
}); });
}); });
};
export const requestDiscordClientClose = () => {
return new Promise((res) => {
io.timeout(25000).emit('request_client_close');
res();
});
}; };

View File

@@ -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'; import { openOP25, closeOP25 } from '../op25Handler/op25Handler.mjs';
let activeDiscordClient = undefined; let activeDiscordClient = undefined;
@@ -42,14 +42,15 @@ export const joinDiscordVC = async (joinData) => {
export const leaveDiscordVC = async (guildId) => { export const leaveDiscordVC = async (guildId) => {
console.log("Leave requested"); console.log("Leave requested");
if (await checkIfConnectedToVC(guildId)) { if (await checkIfConnectedToVC(guildId)) {
await leaveVoiceChannel(guildId, async (clientRemainsOpen) => { const clientRemainsOpen = await leaveVoiceChannel(guildId);
if (!clientRemainsOpen) { console.log("Client should remain open: ", clientRemainsOpen);
console.log("There are no open VC connections"); if (!clientRemainsOpen) {
// TODO DELETE comment DEV ONLY await closeOP25(); 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();
}) }
} }
} }