import { io } from "socket.io-client"; import { connectToChannel, initDiscordBotClient, getVoiceChannelFromID, checkIfConnectedToVC } from '../discordAudioBot/dab.mjs'; import { logIntoServerWrapper, sendNodeUpdateWrapper } from "./socketClientWrappers.mjs"; export const initSocketConnection = async (localNodeConfig) => { const serverEndpoint = `http://${localNodeConfig.serverIp}:${localNodeConfig.serverPort}` || 'http://localhost:3000'; // Adjust the server endpoint const socket = io.connect(serverEndpoint); socket.on('connect', async () => { console.log('Connected to the server'); await logIntoServerWrapper(socket, localNodeConfig); }); socket.on('node-join', async (joinData) => { console.log("Join requested: ", joinData) // TODO - Implement logic to control OP25 for the requested channel/system // Join the requested channel with the requested ID initDiscordBotClient(joinData.clientID, joinData.system, client => { getVoiceChannelFromID(client, joinData.channelID).then(vc => { const connection = connectToChannel(vc); console.log("Bot Connected to VC"); }) }); }); socket.on('node-leave', async () => { console.log("Leave requested"); const connection = await getVoiceConnection(myVoiceChannel.guild.id); if (connection) { console.log("There is an open VC connection, closing it now"); connection.destroy(); } }); socket.on('node-check-connected-status', async (guildId, socketCallback) => { console.log("Requested status check"); if (await checkIfConnectedToVC(guildId)) { console.log("There is an open VC connection"); socketCallback(true); } else { socketCallback(false); } }); socket.on('disconnect', () => { console.log('Disconnected from the server'); }); return socket; }