Functional joining and leaving
- Needs to be tested on multiple servers - Needs to be tested with multiple nodes #1 #9
This commit is contained in:
@@ -97,6 +97,10 @@ export async function checkIfConnectedToVC(guildId) {
|
||||
return connection
|
||||
}
|
||||
|
||||
export const getVoiceConnectionFromGuild = async (guildId) => {
|
||||
return getVoiceConnection(guildId);
|
||||
}
|
||||
|
||||
export async function initDiscordBotClient(token, systemName, readyCallback) {
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.MessageContent],
|
||||
@@ -104,10 +108,16 @@ export async function initDiscordBotClient(token, systemName, readyCallback) {
|
||||
|
||||
client.on(Events.ClientReady, () => {
|
||||
console.log('discord.js client is ready!');
|
||||
|
||||
// Attach the recorder to the VC connection
|
||||
attachRecorder();
|
||||
|
||||
// Set the activity of the bot user
|
||||
client.user.setPresence({
|
||||
activities: [{ name: `${systemName}`, type: ActivityType.Listening }],
|
||||
});
|
||||
|
||||
//
|
||||
readyCallback(client);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,49 +1,61 @@
|
||||
import { io } from "socket.io-client";
|
||||
import { connectToChannel, initDiscordBotClient, getVoiceChannelFromID, checkIfConnectedToVC } from '../discordAudioBot/dab.mjs';
|
||||
import { logIntoServerWrapper, sendNodeUpdateWrapper } from "./socketClientWrappers.mjs";
|
||||
import { connectToChannel, initDiscordBotClient, getVoiceChannelFromID, checkIfConnectedToVC, getVoiceConnectionFromGuild } from '../discordAudioBot/dab.mjs';
|
||||
import { logIntoServerWrapper, sendNodeUpdateWrapper, nodeCheckStatus } from "./socketClientWrappers.mjs";
|
||||
|
||||
/**
|
||||
* Initialize the socket connection with the server, this will handle disconnects within itself
|
||||
* @param {Object} localNodeConfig The local node config object
|
||||
* @returns {any}
|
||||
*/
|
||||
export const initSocketConnection = async (localNodeConfig) => {
|
||||
const serverEndpoint = `http://${localNodeConfig.serverIp}:${localNodeConfig.serverPort}` || 'http://localhost:3000'; // Adjust the server endpoint
|
||||
|
||||
const socket = io.connect(serverEndpoint);
|
||||
|
||||
const discordClients = {};
|
||||
|
||||
socket.on('connect', async () => {
|
||||
console.log('Connected to the server');
|
||||
await logIntoServerWrapper(socket, localNodeConfig);
|
||||
});
|
||||
|
||||
socket.on('node-join', async (joinData) => {
|
||||
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 => {
|
||||
// Add the client object to the IO instance
|
||||
discordClients[vc.guild.id] = client;
|
||||
const connection = connectToChannel(vc);
|
||||
console.log("Bot Connected to VC");
|
||||
})
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('node-leave', async () => {
|
||||
socket.on('node-leave', async (guildId) => {
|
||||
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();
|
||||
if (await checkIfConnectedToVC(guildId)) {
|
||||
const connection = await getVoiceConnectionFromGuild(guildId);
|
||||
if (connection) {
|
||||
console.log("There is an open VC connection, closing it now");
|
||||
// Destroy the open VC connection
|
||||
connection.destroy();
|
||||
|
||||
// Remove the client from the socket connection
|
||||
delete discordClients[guildId];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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('node-get-discord-username', async (guildId, socketCallback) => {
|
||||
console.log("Requested username");
|
||||
socketCallback(discordClients[guildId].user.username);
|
||||
});
|
||||
|
||||
socket.on('node-check-connected-status', nodeCheckStatus);
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('Disconnected from the server');
|
||||
});
|
||||
|
||||
@@ -1,11 +1,60 @@
|
||||
import { checkIfConnectedToVC } from '../discordAudioBot/dab.mjs';
|
||||
|
||||
/**
|
||||
* Wrapper to log into the server
|
||||
* @param {any} socket The socket connection with the server
|
||||
* @param {object} localNodeConfig The local node object
|
||||
* @returns {any}
|
||||
*/
|
||||
export const logIntoServerWrapper = async (socket, localNodeConfig) => {
|
||||
// Log into the server
|
||||
socket.emit("node-login", localNodeConfig.node);
|
||||
|
||||
// Send an update to the server
|
||||
sendNodeUpdateWrapper(socket, localNodeConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the server an update
|
||||
* @param {any} socket The socket connection with the server
|
||||
* @param {object} localNodeConfig The local node object
|
||||
*/
|
||||
export const sendNodeUpdateWrapper = async (socket, localNodeConfig) => {
|
||||
socket.emit('node-update', {
|
||||
'node': localNodeConfig.node,
|
||||
'nearbySystems': localNodeConfig.nearbySystems
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export const nodeJoinServer = 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 => {
|
||||
// Add the client object to the IO instance
|
||||
discordClients[vc.guild.id] = client;
|
||||
const connection = connectToChannel(vc);
|
||||
console.log("Bot Connected to VC");
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the bot is connected to a discord VC in the given server
|
||||
* @param {string} guildId The guild id to check the connection status in
|
||||
* @param {any} socketCallback The callback function to return the result to
|
||||
* @returns {any}
|
||||
*/
|
||||
export const nodeCheckStatus = 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user