Home > Mobile >  discord.js help to read out .txt file
discord.js help to read out .txt file

Time:01-03

I have a one simple Problem with discord.js. So i try to create a Discord Bot with Javascript for the first time and i ran into one Problem with that. I want to read out a .txt file and it works pretty good but now i want to send the Data back with an Embed.

if(command === 'akte') {
        embed.setTitle("Akten Auskunft")
        var Url = null
        if(args.length < 3) {
            embed.setDescription("Bitte tragen sie den Vor- und Nachnamen ein! \t\n Benutzen sie den Syntax: !Akte [Vorname] [Nachname]")
        } else {
            var path = `./Datenbank/${args[1]}.${args[2]}/`
            if(fs.existsSync(path)){
                fs.readdir(path, (err, files) => {
                    files.forEach(file => {
                        if(file === 'Bild.txt') {
                            return
                        }
                        textt = fs.readFileSync(path   file).toString('utf-8');
                        console.log(textt)
                
                        embed.addField(file , textt, false)
                    })
                  })
                } else {
                    embed.setDescription("Es gibt noch keine Akte über  "   args[1]   " "   args[2]   "!");
                }
            }
            message.channel.send(embed)
        }

The Output to the console works fine but i cant write the Data into an Embed

CodePudding user response:

You are using fs#readdir to read the directory, but this function is async, so the callback can be (and in this case is) fired after the code that is after it, so you are adding the fields to the embed after it's already sent.
To solve this, move the line to send the embed inside the last else and add the same line at the end of the callback and into the second if.

if(command === 'akte') {
        embed.setTitle("Akten Auskunft")
        var Url = null
        if(args.length < 3) {
            embed.setDescription("Bitte tragen sie den Vor- und Nachnamen ein! \t\n Benutzen sie den Syntax: !Akte [Vorname] [Nachname]")
            message.channel.send(embed)
        } else {
            var path = `./Datenbank/${args[1]}.${args[2]}/`
            if(fs.existsSync(path)){
                fs.readdir(path, (err, files) => {
                    files.forEach(file => {
                        if(file === 'Bild.txt') {
                            return
                        }
                        textt = fs.readFileSync(path   file).toString('utf-8');
                        console.log(textt)
                
                        embed.addField(file , textt, false)
                    })
                    message.channel.send(embed)
                  })
                } else {
                    embed.setDescription("Es gibt noch keine Akte über  "   args[1]   " "   args[2]   "!");
                    message.channel.send(embed)
                }
            }
        }
  • Related