Search code examples
javascriptnode.jsdiscorddiscord.jsbots

list variables in an embed discord.js


I want to list every "action" variables from my file in an embed. The embed should look like this:

Title: Action list
description: 
action1
action2
action3
... and so on

Here is what I got:

  if(message.content.startsWith(`${config.prefix}actionlist`)){
    let list
    let actionsFile = JSON.parse(fs.readFileSync("./data/actions.json"))
    actionsFile.forEach(action => {
        //push action.name in the list

    })
    const embed = new MessageEmbed() //embed 
    .setColor(`#FF0000`)
    .setTitle('Action list')
    .setDescription(`${list}`)
    message.channel.send({ embeds: [embed] });
    

  }

An action looks like this:

  {
    "name": "shoes",
    "price": 154
  }

Thanks for your help!


Solution

  • Map each item to their name, join them by a line break and use the returning value.

    const actions = actionsFile
       .map(action => action.name)
       .join('\n');
    
    ...
    
    .setDescription(actions);