UNPKG

fenixenbot.js

Version:

Libreria Creacion De Bots De Discord

67 lines (54 loc) 2.17 kB
const fs = require('fs'); const path = require('path'); const YAML = require('yaml'); const Color = require('./Color'); class YamlHandler { constructor(directory) { this.directory = directory; this.data = {}; this.loadFilesRecursively(this.directory); } loadFilesRecursively(currentDir) { if (!fs.existsSync(currentDir)) { console.warn( Color.reset, `La carpeta YAML ${Color.yellow}'${currentDir}' ${Color.reset}no existe. Omitiendo carga en esta ruta.`, Color.reset ); return; } const filesAndDirs = fs.readdirSync(currentDir); for (const item of filesAndDirs) { const itemPath = path.join(currentDir, item); const stat = fs.statSync(itemPath); if (stat.isDirectory()) { this.loadFilesRecursively(itemPath); } else if (item.endsWith('.yml') || item.endsWith('.yaml')) { try { const fileContents = fs.readFileSync(itemPath, 'utf8'); const parsedData = YAML.parse(fileContents); const relativePath = path.relative(this.directory, itemPath); const key = relativePath.substring(0, relativePath.length - path.extname(relativePath).length).replace(/\\/g, '/'); // Elimina extensión y reemplaza \\ por / // Elimina la extensión this.data[key] = parsedData // Log con la ruta completa } catch (error) { } } } } getYml(nameFolder) { return this.data[nameFolder] || {}; } } let globalYamlHandler = null; function Yaml(options) { if (!options || !options.nameFolder) { throw new Error( Color.red, 'Debes proporcionar una carpeta para los archivos YAML.', Color.reset ); } globalYamlHandler = new YamlHandler(options.nameFolder); global.yml = (nameFolder) => globalYamlHandler.getYml(nameFolder); } module.exports = { Yaml };