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 { 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])
});
});
};
@@ -95,3 +100,11 @@ export const requestDiscordID = () => {
});
});
};
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';
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();
}
}
}