Search code examples
javascriptnode.jsdiscord.jscommando

How to get values from object without any "name"?


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?


Solution

  • 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
          }
        });
      }
    }