const { readdirSync } = require("fs") const { join } = require("path") const logger = require("../functions/logger") /** * Loads and registers all function modules from the functions directory * @param {Object} client - The client instance * @returns {Promise} */ module.exports = async (client) => { try { if (!client || !client.functions) { throw new Error('Invalid client object or missing functions collection'); } const functionsDir = join(__dirname, '..', 'functions'); const functions = readdirSync(functionsDir) .filter(file => file.endsWith('.js') && file !== 'logger.js'); const loadedFunctions = await Promise.all( functions.map(async (file) => { try { const functionName = file.split('.')[0]; const functionModule = require(join(functionsDir, file)); client.functions.set(functionName, functionModule); return functionName; } catch (error) { logger.error('Function_Handler', `Failed to load function ${file}: ${error.message}`); return null; } }) ); const successfulLoads = loadedFunctions.filter(Boolean).length; logger.event('Function_Handler', `Successfully loaded ${successfulLoads}/${functions.length} functions`); } catch (error) { logger.error('Function_Handler', `Failed to initialize function handler: ${error.message}`); throw error; } };