Getting the values from a JSON into a discord.js embed

Viewed 1334

I'm making a discord.js bot where all the command data is stored in a JSON file and when the help command is run, I want it to iterate through the JSON, at the level the user entered.

So far, I've managed to check if the value entered is a command or a category, and I have been able to console.log all the information and it shows the correct data I want to display on the embed.

For example, in my JSON file I have:

{
  "category1": {
    "command1": {
      "Aliases": [],
      "Description": "Runs a command"
    }
  }
}

When I do the console.log() when the user runs g!help category1 it outputs:

command1
Runs a command

and when it sends the embed I get:

[object Object]
undefined

Here is the part of the code which iterates through the JSON and adds the data to the embed:

for (ctg in help_data) {
  if (args[0] === ctg) {
    embed.setTitle(ctg)
    for (command in help_data[ctg]) {
      var cmd = command
      var desc = help_data[ctg][command].Description

      console.log(cmd)
      console.log(desc)

      embed.addField({
        "name": cmd,
        "Value": desc
      })
    }
  }
}

I don't understand why it changes between the console.log and the embed.addField, if someone could please explain why this happens and how I can fix it, that would be amazing!

1 Answers

embed.addField takes three parameters, the first is the name of the field, the second is its value. Both of these are strings. In your example, your first argument is an object that gets converted to a string (and hence becomes [object Object]) and the second one (as you don't provide anything) is undefined.

To fix this, you can either add the two strings like this:

embed.addField(cmd, desc);

Or use the addFields method instead, that accepts an object like this:

embed.addFields({
  name: cmd,
  value: desc,
});
Related