Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda