110 lines
3.7 KiB
JavaScript
110 lines
3.7 KiB
JavaScript
/**
|
|
* Reloads a module (event, function, or command) in the client
|
|
* @param {Object} client - The Discord client instance
|
|
* @param {string} category - The category of the module ('events', 'functions', or command name)
|
|
* @param {string} [name] - The name of the module (required for events and functions)
|
|
* @returns {string} Status message indicating success or failure
|
|
*/
|
|
function Reload(client, category, name) {
|
|
// Input validation
|
|
if (!client) throw new Error('Client instance is required');
|
|
if (!category) return 'Provide a category/command name to reload!';
|
|
|
|
const reloaders = {
|
|
events: () => reloadEvent(client, name),
|
|
functions: () => reloadFunction(client, name),
|
|
default: () => reloadCommand(client, category)
|
|
};
|
|
|
|
return (reloaders[category] || reloaders.default)();
|
|
}
|
|
|
|
/**
|
|
* Reloads an event module
|
|
* @param {Object} client - The Discord client instance
|
|
* @param {string} name - The name of the event
|
|
* @returns {string} Status message
|
|
*/
|
|
function reloadEvent(client, name) {
|
|
if (!name) return 'Provide an event name to reload!';
|
|
|
|
try {
|
|
const eventPath = `../events/${name}.js`;
|
|
delete require.cache[require.resolve(eventPath)];
|
|
const eventModule = require(eventPath);
|
|
|
|
// Remove existing event listener
|
|
const existingHandler = client._events[name];
|
|
if (existingHandler) {
|
|
client.off(name, typeof existingHandler === 'function' ? existingHandler : existingHandler[0]);
|
|
}
|
|
client.event.delete(name);
|
|
|
|
// Add new event listener
|
|
const boundHandler = eventModule.bind(null, client);
|
|
client.on(name, boundHandler);
|
|
client.event.set(name, boundHandler);
|
|
|
|
return `Reloaded event: **${name}**.js`;
|
|
} catch (error) {
|
|
return `Couldn't reload: **events/${name}**\n**Error**: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reloads a function module
|
|
* @param {Object} client - The Discord client instance
|
|
* @param {string} name - The name of the function
|
|
* @returns {string} Status message
|
|
*/
|
|
function reloadFunction(client, name) {
|
|
if (!name) return 'Provide a function name to reload!';
|
|
|
|
try {
|
|
const functionPath = `../functions/${name}.js`;
|
|
delete require.cache[require.resolve(functionPath)];
|
|
const functionModule = require(functionPath);
|
|
|
|
client.functions.delete(name);
|
|
client.functions.set(name, functionModule);
|
|
|
|
return `Reloaded function: **${name}**.js`;
|
|
} catch (error) {
|
|
return `Couldn't reload: **functions/${name}**\n**Error**: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reloads a command module
|
|
* @param {Object} client - The Discord client instance
|
|
* @param {string} commandName - The name of the command
|
|
* @returns {string} Status message
|
|
*/
|
|
function reloadCommand(client, commandName) {
|
|
try {
|
|
const commandPath = `../commands/${commandName}.js`;
|
|
delete require.cache[require.resolve(commandPath)];
|
|
const commandModule = require(commandPath);
|
|
|
|
// Handle aliases
|
|
const existingCommand = client.commands.get(commandName);
|
|
if (existingCommand?.config?.aliases) {
|
|
existingCommand.config.aliases.forEach(alias => client.aliases.delete(alias));
|
|
}
|
|
|
|
// Update command
|
|
client.commands.delete(commandName);
|
|
client.commands.set(commandName, commandModule);
|
|
|
|
// Update aliases if they exist
|
|
if (commandModule.config?.aliases) {
|
|
commandModule.config.aliases.forEach(alias => client.aliases.set(alias, commandName));
|
|
}
|
|
|
|
return `Reloaded command: **commands/${commandName}**.js`;
|
|
} catch (error) {
|
|
return `Couldn't reload: **commands/${commandName}**\n**Error**: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
module.exports = Reload; |