Kashix.
Kashix.

Reputation: 33

Add Field data from an array to an Embed once created

Edit: ALREADY FIXED. THANKS TO EVERYONE

I'm trying to add Fields to an Embed once it's created from a for because I wanna add values that are saved in arrays.

I've tried next codes, no one worked

for(let i = 0; i < nombres.length; i++){
            embed.addField(nombres[i], descripciones[i], true)
        }
for(let i = 0; i < nombres.length; i++){
            embed = {
                fields: {
                    name: nombres[i],
                    value: descripciones[i],
                    inline: true,
                },
            };
        }
for(let i = 0; i < nombres.length; i++){
            embed.fields(nombres[i], descripciones[i], true)
        }
for(let i = 0; i < nombres.length; i++){
            embed.addFields({name: nombres[i], value: descripciones[i], inline: true});
        }

The thing is, I wanna do something like this but that works

let nombres = [];
let descripciones = [];

const embed = new Discord.MessageEmbed()
.setDescription('All commands available.')

for(let i = 0; i < nombres.length; i++){
   embed.addField(nombres[i], descripciones[i], true)
}

And what I get is only this
enter image description here

Upvotes: 2

Views: 996

Answers (1)

Supesu
Supesu

Reputation: 676

Try this code out, this should work

const { MessageEmbed } = require("discord.js")

let array1 = ["1", "2", "3", "4"]
let array2 = ["one", "two", "three", "four"]

var embed = new MessageEmbed();

embed.setDescription('test')

for (let i = 0; i < array1.length; i++) 
{
   embed.addField(array1[i], array2[i], false)
}

message.channel.send(embed)

enter image description here

// imports
const Discord = require("discord.js");
const fs = require("fs").promises;
const fss = require("fs");
const path = require("path");

// exports
module.exports = {
    name: "commands",
    alias: ["commands"],
    description: "",
    run: (client, message, args) => {

        let nombres = [],
            descripciones = [];

        fs.readdir(path.join(__dirname))
            .then(filess => {
                x = 0;
                filess.forEach(fil => {
                    if (!fil.endsWith('.js')) return;
                    let commandName = fil.substring(0, fil.indexOf('.js'));
                    let commandModule = require(path.join(__dirname, commandName));

                    nombres[x] = commandName;
                    descripciones[x] = commandModule.description;
                    x++
                    console.log(x, nombres[x], descripciones[x])
                })
            })


        var embed = new Discord.MessageEmbed();

        embed.setDescription('All commands available.')

        for (let i = 0; i < nombres.length; i++) {
            embed.addField(nombres[i], descripciones[i], false)
        }

        console.log({ embed })
        message.channel.send(embed)
    }
}

Upvotes: 1

Related Questions