2025-04-25 21:27:00 -04:00
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
const rootDir = path.dirname(__dirname);
|
|
|
|
|
|
|
|
|
|
// Load modules function - hot reload functionality removed
|
|
|
|
|
export const loadModules = async (clientConfig, client) => {
|
2025-05-08 01:52:12 +00:00
|
|
|
const modules = clientConfig.modules || [];
|
|
|
|
|
const modulesDir = path.join(rootDir, '_opt');
|
2025-04-25 21:27:00 -04:00
|
|
|
|
2025-05-08 01:52:12 +00:00
|
|
|
// Create opt directory if it doesn't exist
|
|
|
|
|
if (!fs.existsSync(modulesDir)) {
|
|
|
|
|
fs.mkdirSync(modulesDir, { recursive: true });
|
|
|
|
|
}
|
2025-04-25 21:27:00 -04:00
|
|
|
|
2025-05-08 01:52:12 +00:00
|
|
|
client.logger.info(`[module:loader] Loading modules: ${modules.join(', ')}`);
|
|
|
|
|
// Load each module
|
|
|
|
|
for (const moduleName of modules) {
|
|
|
|
|
try {
|
|
|
|
|
// Try _opt first, then fallback to core _src modules
|
|
|
|
|
let modulePath = path.join(modulesDir, `${moduleName}.js`);
|
|
|
|
|
if (!fs.existsSync(modulePath)) {
|
|
|
|
|
// Fallback to core source directory
|
|
|
|
|
modulePath = path.join(rootDir, '_src', `${moduleName}.js`);
|
|
|
|
|
if (!fs.existsSync(modulePath)) {
|
|
|
|
|
client.logger.warn(`[module:loader] Module not found: ${moduleName}.js`);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-25 21:27:00 -04:00
|
|
|
|
2025-05-08 01:52:12 +00:00
|
|
|
// Import module (using dynamic import for ES modules)
|
|
|
|
|
// Import module
|
|
|
|
|
const moduleUrl = `file://${modulePath}`;
|
|
|
|
|
const module = await import(moduleUrl);
|
2025-04-25 21:27:00 -04:00
|
|
|
|
2025-05-08 01:52:12 +00:00
|
|
|
// Register commands if the module has them
|
|
|
|
|
if (module.commands) {
|
|
|
|
|
if (Array.isArray(module.commands)) {
|
|
|
|
|
// Handle array of commands
|
|
|
|
|
for (const command of module.commands) {
|
|
|
|
|
if (command.data && typeof command.execute === 'function') {
|
|
|
|
|
const commandName = command.data.name || command.name;
|
|
|
|
|
client.commands.set(commandName, command);
|
|
|
|
|
client.logger.info(`[module:loader] Registered command: ${commandName}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (typeof module.commands === 'object') {
|
|
|
|
|
// Handle map/object of commands
|
|
|
|
|
for (const [commandName, command] of Object.entries(module.commands)) {
|
|
|
|
|
if (command.execute && typeof command.execute === 'function') {
|
|
|
|
|
client.commands.set(commandName, command);
|
|
|
|
|
client.logger.info(`Registered command: ${commandName}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-25 21:27:00 -04:00
|
|
|
|
2025-05-08 01:52:12 +00:00
|
|
|
// Call init function if it exists
|
|
|
|
|
if (typeof module.init === 'function') {
|
|
|
|
|
await module.init(client, clientConfig);
|
|
|
|
|
client.logger.info(`[module:loader] Module initialized: ${moduleName}`);
|
|
|
|
|
} else {
|
|
|
|
|
client.logger.info(`[module:loader] Module loaded (no init): ${moduleName}`);
|
|
|
|
|
}
|
2025-04-25 21:27:00 -04:00
|
|
|
|
2025-05-08 01:52:12 +00:00
|
|
|
// Store the module reference (this isn't used for hot reloading anymore)
|
|
|
|
|
client.modules = client.modules || new Map();
|
|
|
|
|
client.modules.set(moduleName, module);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
client.logger.error(`[module:loader] Failed to load module ${moduleName}: ${error.message}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-25 21:27:00 -04:00
|
|
|
};
|