Files
DRBv3/client/discordAudioBot/dabWrappers.mjs
Logan Cusano fea7ed2c7f #16
- Server can now choose from IDs in the DB
- Implemented an active system to disable some IDs from being used
2024-03-24 02:45:34 -04:00

135 lines
4.7 KiB
JavaScript

import { connectToChannel, checkIfConnectedToVC, initDiscordBotClient, getVoiceChannelFromID, getVoiceConnectionFromGuild } from './dab.mjs';
import { openOP25, closeOP25 } from '../op25Handler/op25Handler.mjs';
let activeDiscordClient = undefined;
const activeDiscordVoiceConnections = {};
/**
* Join the requested server VC and listen to the requested system
* @param {object} joinData The object containing all the information to join the server
*/
export const joinDiscordVC = async (joinData) => {
console.log("Join requested: ", joinData)
const connection = await new Promise((res) => {
// Check if a client already exists
if (!activeDiscordClient) {
// Open a new client and join the requested channel with the requested ID
initDiscordBotClient(joinData.clientID, joinData.system, client => {
// Open an instance of OP25
openOP25(joinData.system);
getVoiceChannelFromID(client, joinData.channelID).then(vc => {
// Add the client object to the IO instance
activeDiscordClient = client;
const connection = connectToChannel(vc);
activeDiscordVoiceConnections[vc.guild.id] = connection;
console.log("Bot Connected to VC");
res(connection);
});
});
} else {
// Join the requested channel with the requested ID
getVoiceChannelFromID(activeDiscordClient, joinData.channelID).then(vc => {
// Add the client object to the IO instance
const connection = connectToChannel(vc);
activeDiscordVoiceConnections[vc.guild.id] = connection;
console.log("Bot Connected to VC");
res(connection);
});
}
});
return connection;
}
/**
* Leave VC on the requested server
* @param {string} guildId The guild ID to disconnect from VC
*/
export const leaveDiscordVC = async (guildId) => {
console.log("Leave requested");
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 connection from the object
delete activeDiscordVoiceConnections[guildId];
// Check if this was the last open VC connection
if (Object.keys(activeDiscordVoiceConnections).length == 0) {
console.log("No more open VC connections, closing the client-side discord client and OP25")
// Close the active client if there are no open VCs after this one
activeDiscordClient.destroy();
activeDiscordClient = undefined;
// Close OP25
await closeOP25();
}
}
}
}
/**
* 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
* @returns {boolean} If the node is connected to VC in the given guild
*/
export const checkIfDiscordVCConnected = async (guildId) => {
console.log("Requested status check");
if (await checkIfConnectedToVC(guildId)) {
console.log("There is an open VC connection");
return (true);
} else {
return (false);
}
}
/**
* Get the username of the bot in a given guild
* (there may be a server nickname given to the bot in a certain guild)
* @param {string} guildId The guild id to check the connection status in
* @returns {string} The username of the bot in the given guild's VC
*/
export const getDiscordUsername = async (guildId) => {
console.log("Requested username");
if (activeDiscordClient) {
// Fetch the guild
const guild = await client.guilds.fetch(guildId);
// Fetch the bot member in the guild
const botMember = await guild.members.fetch(client.user.id);
// Return bot's nickname if available, otherwise return username
return botMember.nickname || botMember.user.username;
}
else return (undefined);
}
/**
* Get the ID of the currently running bot
* @returns {string} The ID of the active client
*/
export const getDiscordID = async () => {
console.log("Requested username");
if (activeDiscordClient) {
return (activeDiscordClient.user.id);
}
else return (undefined);
}
/**
* Check if there is an open discord client
* @returns {boolean} If the client is open or not
*/
export const checkIfClientIsOpen = async () => {
if (activeDiscordClient) {
return (true);
}
return (false);
}