const { readdirSync } = require("fs") const path = require("path") const color = require("../functions/colorCodes") /** * Loads and registers all event handlers for the client * @param {Object} client - The Discord client instance * @returns {Promise} */ module.exports = async (client) => { try { const eventsPath = path.join(__dirname, "..", "events") const eventFiles = readdirSync(eventsPath) .filter(file => file.endsWith(".js")) const loadedEvents = await Promise.all( eventFiles.map(async (file) => { try { const eventName = path.parse(file).name const event = require(path.join(eventsPath, file)) // Register the event client.event.set(eventName, event.bind(null, client)) client.on(eventName, event.bind(null, client)) return eventName } catch (error) { console.error(color("%", `%r[Event_Handler]%7 :: Failed to load event %e${file}%7: ${error.message}`)) return null } }) ) const successfulEvents = loadedEvents.filter(Boolean) console.log(color("%", `%b[Event_Handler]%7 :: Successfully loaded %e${successfulEvents.length}%7 events`)) if (successfulEvents.length < eventFiles.length) { console.warn(color("%", `%y[Event_Handler]%7 :: %r${eventFiles.length - successfulEvents.length}%7 events failed to load`)) } } catch (error) { console.error(color("%", `%r[Event_Handler]%7 :: Critical error: ${error.message}`)) throw error // Re-throw to handle it in the main application } }