import { checkIfNodeIsConnectedToVC, getNodeDiscordID, getNodeDiscordUsername } from '../../modules/socketServerWrappers.mjs'; import { getAllDiscordIDs } from '../../modules/mongo-wrappers/mongoDiscordIDWrappers.mjs' export const checkOnlineBotsInGuild = async (nodeIo, guildId) => { let onlineBots = []; const openSockets = [...await nodeIo.allSockets()]; await Promise.all(openSockets.map(async openSocket => { openSocket = await nodeIo.sockets.sockets.get(openSocket); const connected = await checkIfNodeIsConnectedToVC(nodeIo, guildId, openSocket.node.nuid); console.log("Connected:", connected); if (connected) { const username = await getNodeDiscordUsername(openSocket, guildId); const discordID = await getNodeDiscordID(openSocket); onlineBots.push({ name: username, discord_id: discordID, nuid: openSocket.node.nuid }); } })); return onlineBots; } export const getAvailableTokensInGuild = async (nodeIo, guildId) => { try { // Execute both asynchronous functions concurrently const [discordIDs, onlineBots] = await Promise.all([ getAllDiscordIDs(), // Fetch all Discord IDs checkOnlineBotsInGuild(nodeIo, guildId) // Check online bots in the guild ]); // Use the results of both promises here console.log("Available Discord IDs:", discordIDs); console.log("Online bots in the guild:", onlineBots); // Filter any discordIDs that are not active const availableDiscordIDs = discordIDs.filter(discordID => discordID.active == true).filter(discordID => !onlineBots.some(bot => Number(bot.discord_id) == discordID.discord_id)); // Return the unavailable discordIDs return availableDiscordIDs; } catch (error) { console.error('Error getting available tokens in guild:', error); throw error; } };