简体   繁体   中英

Reload command issue in Discord

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) {
            console.error(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

Add a second argument for the foldername in your command then do

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

and run the command like !cmd commandname foldername

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM