0

I've got a piece of code in JS:

var commands = require('commands.json');
module.exports = class CatCommand extends commando.Command {
    constructor(client) {
        super(client, {
            name: 'cat',
            group: 'fun',
            aliases: commands[this.group][this.name].aliases || [utils.translate(this.name)],
            memberName: name,
            description: commands[this.group][this.name].description || 'No description',
            examples: commands[this.group][this.name].usages || ['No examples'],
            throttling: {
                usages: 2,
                duration: 5
            }
        });
    }

    async run(message) {}
}

I need to use variables name and group as commands' key. What's the best way to do that?

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Lukser
  • 3
  • 1
  • There is no way to refer to fields of an "under construction" object literal. If you need to use such values, make them values of separately-declared variables. – Pointy Dec 26 '18 at 15:00
  • Okay, I'll try that. Thank you! – Lukser Dec 26 '18 at 15:01

1 Answers1

0

Since you're constructing that object, you can't get those properties (they technically don't exist yet).
Try declaring those values in a variable outside of the constructor, like this:

var name = "cat",
  group = "fun";

module.exports = class CatCommand extends commando.Command {
  constructor(client) {
    super(client, {
      name,
      group,
      aliases: commands[group][name].aliases || [utils.translate(name)],
      memberName: name,
      description: commands[group][name].description || 'No description',
      examples: commands[group][name].usages || ['No examples'],
      throttling: {
        usages: 2,
        duration: 5
      }
    });
  }
}
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50