Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
Discord.JS - List all files within a directory as one message

I am having an issue where I cannot seem to find a solution.

I have written a Discord bot from Discord.JS that needs to send a list of file names from a directory as one message. So far I have tried using fs.readddir with path.join and fs.readfilesync(). Below is one example.

        const server = message.guild.id;
        const serverpath = `./sounds/${server}`;
        const fs = require('fs');
        const path = require('path');

        const directoryPath = path.join('/home/pi/Pablo', serverpath);

        fs.readdir(directoryPath, function(err, files) {
            if (err) {
                return console.log('Unable to scan directory: ' + err);
            }
            files.forEach(function(file) {
                message.channel.send(file);
            });
        });

Whilst this does send a message of every file within the directory, it sends each file as a separate message. This causes it to take a while due to Discord API rate limits. I want them to all be within the same message, separated with a line break, with a max of 2000 characters (max limit for Discord messages).

Can someone assist with this?

Thanks in advance.

Jake

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I recommend using fs.readdirSync(), it will return an array of the file names in the given directory. Use Array#filter() to filter the files down to the ones that are JavaScript files (extentions ending in ".js"). To remove ".js" from the file names use Array#map() to replace each ".js" to "" (effectively removing it entirely) and use Array#join() to join them into a string and send.

const server = message.guild.id;
const serverpath = `./sounds/${server}`;
const { readdirSync } = require('fs');
const path = require('path');

const directoryPath = path.join('/home/pi/Pablo', serverpath);

const files = readdirSync(directoryPath)
   .filter(fileName => fileName.endsWith('.js'))
   .map(fileName => fileName.replace('.js', ''));
   .join('\n');
message.channel.send(files);

Regarding handling the sending of a message greater than 2000 characters: You can use the Util.splitMessage() method from Discord.JS and provide a maxLength option of 2000. As long as the number of chunks needed to send is not more than a few you should be fine from API ratelimits

const { Util } = require('discord.js');

// Defining "files"

const textChunks = Util.splitMessage(files, {
   maxLength: 2000
});

textChunks.forEach(async chunk => {
   await message.channel.send(chunk);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Built an array of strings (names of files) then join with "\n".

let names = []
fs.readdir(directoryPath, function(err, files) {
    if (err) {
        return console.log('Unable to scan directory: ' + err);
    }
    files.forEach(function(file) {
        names << file
        
    });
});
message.channel.send(names.join("\n"));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda