#I have a code fragment that I don't understand can someone explain me pls

9 messages · Page 1 of 1 (latest)

grave fableBOT
#

See #faq to learn how to properly share code on Discord: #faq message

weary root
#

I try to edit the code but I don't know wich part.

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require(filePath);
        // Set a new item in the Collection with the key as the command name and the value as the exported module
        if ('data' in command && 'execute' in command) {
            client.commands.set(command.data.name, command);
        } else {
            console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
        }
    }
}
#

My goal is to put my .js files directly in the /commands folder. And not a folder in a folder and then my .js

#

Can someone help me tyyy 🙂

sudden lava
#

Still on old require/module.exports style...

const fs = require("node:fs/promises");

//
const commandsDir = await fs.opendir(foldersPath);

for await (const dirent of commandsDir) {
  const { name } = dirent;

  if (!(
    dirent.isFile() &&
    name.endsWith(".js")
  ))
    continue;

  const filePath = path.join(foldersPath, name);

  const command = require(filePath);

  if (
    Object.hasOwn(command, "data") &&
    Object.hasOwn(command, "execute")
  )
    client.commands.set(command.data.name, command);
  else
    console.warn(...);
}```
weary root
sudden lava
#

It should. If you use CJS (require/module.exports), the part containing await must be in an async function but that's it

weary root
#

😅öhm ok