-1

I have some code for my discord js bot. That code should get all commands in commands dir. When import command from commandsDir + files[i]; was let command = require(commandsDir + files[i]) everything was fine but:

getCommands = function () {
  let files = fs.readdirSync(commandsDir).filter(file => (
    !fs.statSync(commandsDir + file).isDirectory() &&
    !file.startsWith("!") &&
    file.endsWith(".js")));
  let commands = {};

  for (let i = 0; i < files.length; i++) {
    try {
      import command from commandsDir + files[i];
      commands[command.name] = command;
    } catch (e) {
      console.error(e);
    }
  }
  return commands;
}
getCommand = function (commandName) {
  if (commands[commandName]) return commands[commandName];

  for (let i in commands)
    if (commands[i].aliases.includes(commandName)) return commands[i];
}

When i run that code i get an SyntaxError: Unexpected identifier error in import command from commandsDir + files[i];: idk why but please help me fix this error (if possible) without any other things. UPD: Here example of my command.js file

module.exports = {
    name: "test",
    aliases: [],
    description: "test i guess",
    usage: "",
    run(message, args) {
        message.channel.send(`hello`);
    }
}

1 Answers1

0

You can find more informations here. But in short you probably need dynamic imports. (call import(commandsDir + files[i])) according to Nicolai Schmid answer.

Berci
  • 2,876
  • 1
  • 18
  • 28