139 lines
3.9 KiB
JavaScript
139 lines
3.9 KiB
JavaScript
// 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';
|
|
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);
|
|
const port = process.env.PDAB_PORT || 3000;
|
|
|
|
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, false, join(__dirname, "./pdab"));
|
|
pdabProcess = true; // TODO - Make this more dynamic
|
|
}
|
|
|
|
export const startPdabSocketServer = () => {
|
|
io.on('connection', (socket) => {
|
|
log.INFO('A user connected');
|
|
|
|
socket.on('disconnect', () => {
|
|
log.INFO('User disconnected');
|
|
});
|
|
|
|
// Listen for the discord client ready event
|
|
socket.on('discord_ready', (message) => {
|
|
log.INFO("Message from local client", message);
|
|
botCallback();
|
|
});
|
|
});
|
|
|
|
server.listen(port, async () => {
|
|
log.INFO(`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) => {
|
|
log.INFO("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) => {
|
|
log.INFO("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) => {
|
|
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) => {
|
|
log.INFO(`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) => {
|
|
log.INFO(`Discord username: ${result[0]}`);
|
|
res(result[0]);
|
|
});
|
|
})
|
|
};
|
|
|
|
export const checkIfClientIsOpen = () => {
|
|
return new Promise((res) => {
|
|
io.timeout(25000).emit('check_client_is_open', (status, result) => {
|
|
log.INFO(`Client is open: ${result}`);
|
|
res(result[0])
|
|
});
|
|
});
|
|
};
|
|
|
|
export const requestDiscordID = () => {
|
|
return new Promise((res) => {
|
|
io.timeout(25000).emit('request_discord_id', (status, result) => {
|
|
log.INFO(`Discord ID: ${result}`);
|
|
res(result[0]);
|
|
});
|
|
});
|
|
};
|
|
|
|
|
|
export const requestDiscordClientClose = () => {
|
|
return new Promise((res) => {
|
|
io.timeout(25000).emit('request_client_close');
|
|
pdabProcess = false;
|
|
res();
|
|
});
|
|
}; |