1

I want to update my commands code without restarting bot fully using nodemon or just node . I wanted to make a command I can send to update all commands in my bot (basically commands collection where they are and their code stored). Here is the code I tried to use:

const { glob } = require('glob')
const { promisify } = require('util')
const globPromise = promisify(glob);

await client.commands.clear()

const commandFiles = await globPromise(`${process.cwd()}/commands/**/*.js`);
commandFiles.map((value) => {
const file = require(value);
const splitted = value.split("/");
const directory = splitted[splitted.length - 2];
    
if (file.name) {
const properties = { directory, ...file };
client.commands.set(file.name, properties);
}
})

Result says my code worked but commands are not updating, how can I fix it? Just in case: my djs version is v13.2.0

MegaMix_Craft
  • 2,199
  • 3
  • 10
  • 36
  • 2
    NodeJS is caching results of scripts execution from `require()` (also known as require cache). You should clear this cache. Maybe this answer will help you: [node.js require() cache - possible to invalidate?](https://stackoverflow.com/a/11477602/16048617) – koloml Oct 21 '21 at 12:02
  • Might this work for your case? https://stackoverflow.com/q/24666696/15325967 – insyri Oct 21 '21 at 12:03
  • 1
    @insyri he want to reload commands without reloading application at all. So he should clear the cache in that case and reload commands using `require` again. – koloml Oct 21 '21 at 12:04
  • Ah I see, I thought the goal specifically was to update the commands remotely. – insyri Oct 21 '21 at 12:13
  • @koloml It helped, but is there any way to get commands from `client.commands` and update them one by one, or maybe get commands using fs? (I have no idea how to do it using fs because I have category folders in `commands` folder) – MegaMix_Craft Oct 21 '21 at 12:23

1 Answers1

2

Using this function deletes the cache corresponding to the module/file and gets the updated version:

function requireUncached(module) {
    delete require.cache[require.resolve(module)];
    return require(module);
}

Now, to require the files, just replace require with requireUncached.

You might however need to update on each event, to prevent it being updated late

client.on("messageCreate", msg => {
  client.commands.clear();
  const files = fs.readdirSync("./commands") //read commands folder
  const folders = files.filter(f => !f.includes(".")) //get category folders
  for (const folder of folders) {
    const cmdFiles = fs.readdirSync(`./commands/${folder}`).filter(f => f.endsWith(".js")) //get cmd files in each folder
    for (const file of cmdFiles) {
      const f = requireUncached(`./commands/${folder}/${file}`) //require the file "uncached"
      client.commands.set(f.name, f)
    }
  }
})

There might be more efficient ways to do this, making sure that you don't have to update on each event

MrMythical
  • 8,908
  • 2
  • 17
  • 45