0

The error from the terminal

I’ve been working for a while on making commands for my discord.js bot, but now I am starting to categorize them. My commands are currently being put into a collector for a new discord collection (named client.{commandType}commands). I want to make “{commandType}” to be changed out for the value “i” in the following code. Anyone know how to do this?

folder.forEach(i => {
  fs.readdir(`/home/runner/commands/${i}`, (err, file) => {
    if (err) console.log(err);

    let jsfile = file.filter(f => f.split(`.`).pop() === `js`);

    jsfile.forEach((f,e) => {
      let props = require(`/home/runner/commands/${i}/${f}`);

      client.commandPermissions.set(props.help.name, props.help.permissionLevel);

      console.log(`${props.help.name}: ${props.help.permissionLevel}`);

      client.{commandType}command.set(props.help.name, props);
             ^^^^^^^^^^^^^

    });

  });

});

So how do I fix that?

Edit: Just so I am clear, I am essentially trying to do this: client. + i + commands.set(props.help.name, props);

1 Answer 1

1

You should be able to just use:

client[i + 'command'].set(props.help.name, props);

Unlike many other languages, JavaScript has no difference between an object and a dictionary/hashmap. You are able to use either notation (obj.property or obj['property']) to access a property of an object. The main difference between them is that obj.property looks better, but in obj['property'], 'property' can be a normal string, a variable, or some other expression.

Sign up to request clarification or add additional context in comments.

7 Comments

Hate to say it, but it didnt work. Gave me an error saying unexpected token ‘[’
@Silveradoz did you do client.[i + 'command'] or client[i + 'command']? The latter is the correct syntax.
I used both. Neither one worked sadly. Also, I’ll add a photo of the error.
@Silveradoz I just noticed, is it command or commands? You use both in your question, but I assume only one is correct.
That would be it (Hopefully)! I did use commands. I’ll test it and let you know. > Nope. Says the same error as shown in the top of the post... hmm... Also, am I properly tagging you? With your username I am not entirely sure...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.