Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

javascript - Adding fields to embed in a loop [Discord.js]

i need help because i can't get around my problem. I made a system that loads all files from the indicated folder. I made that each file is 1 field in embed. I did this with a loop and fs.readFile. The problem is that when I want to send an embed the command channel.send (userPartnerEmbed) would have to be in the middle, i.e. in a loop. It sends no 1 embed, but the number of files in the folder. I am asking for help and solution.

Below code:

const partnerFiles = readdirSync(dirPartnerUserFolder).filter((file) => 
        file.endsWith(".partner.yaml"),
      )

      let i = partnerFiles.length

      if(i === 0){
        channel.send(embed)
        return
      }else{

        // Here is my embed 

        var userPartnerEmbed = new MessageEmbed()
        .setTitle(`User - ${userPartnerName}`)
        .setThumbnail(userPartnerAvatar)
        .setColor(0x000000)
        .setDescription("Description")

        for(const data of partnerFiles){
          fs.readFile(dirPartnerUserFolder+`/${data}`, "utf8", (err, jsonString) => {
            if(err){
              console.log("Error:", err)
              return
            }
            const dataPartnerUser = JSON.parse(jsonString)        
            // Here add field to embed
            userPartnerEmbed.addField("ID", dataPartnerUser.ID)
          }) // <- end of readFile
        }
        userPartnerEmbed.setTimestamp()
        userPartnerEmbed.setFooter(guildName)

        // Send embed but without fields
        channel.send(userPartnerEmbed)

I can't read the data behind these parentheses:

}) // <- end of readFile

There is maybe some solution or "taking" the data behind the parentheses and the loop

question from:https://stackoverflow.com/questions/65873970/adding-fields-to-embed-in-a-loop-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...