Discord.js An issue with reload comomand

I'm having an issue with my reload command after changing my commands i used to have my commands stored in ./commands folder and the reload command was working well but i made few changes to this and wanted to organize my commands better and made sub folders for each command category they are now stored like this /commands/fun/commandname.js and changed my main bot file's code to this

const isDirectory = source => fs.lstatSync(source).isDirectory();
const getDirectories = source => fs.readdirSync(source).map(name => path.join(source, name)).filter(isDirectory);

getDirectories(__dirname + '/commands').forEach(category => {
  const commandFiles = fs.readdirSync(category).filter(file => file.endsWith('.js'));

  for(const file of commandFiles) {
    const command = require(`${category}/${file}`);
    client.commands.set(command.name, command);

the reload command's code

module.exports = {
    name: 'reload',
    description: 'Reloads a command',
    args: true,
    usage: '<command name>',
    execute(message, args) {
        const config = require('../config.json');
        if(message.author.id !== config.ownerID) return;
        const commandName = args[0].toLowerCase();
        const command = message.client.commands.get(commandName)
            || message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

        if (!command) {
            return message.channel.send(`There is no command with name or alias \`${commandName}\`, ${message.author}!`);

        delete require.cache[require.resolve(`./${command.name}.js`)];

        try {
            const newCommand = require(`./${command.name}.js`);
            message.client.commands.set(newCommand.name, newCommand);
            message.channel.send(`Reloaded command \`${command.name}\`.`);
        } catch (error) {
            message.channel.send(`There was an error while reloading a command \`${command.name}\`:\n\`${error.message}\``);

when the command is used it throws error "Error: Cannot find module './commandname.js'" i tried to change delete require.cache[require.resolve(`./${command.name}.js`)]; to delete require.cache[require.resolve(`././${command.name}.js`)]; but doesn't seem to work

Read more here: https://stackoverflow.com/questions/65721365/discord-js-an-issue-with-reload-comomand

Content Attribution

This content was originally published by Aro at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: