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

206
Visualizações
How to pick random image from directory (Using Repl.it)

I'm making a banana machine and it grabs a random banana image in a directory i created inside the fun directory heres my code:

var fs = require('fs');
  fs.readdir('./fun/bananas/', (err, files) => {
    if (err) console.log(err);

    let bonannapics = files.filter(f => f.split('.').pop() === 'png');

    let chosenFile = bonannapics[Math.floor(Math.random() * bonannapics.length)]
    const attachment = new MessageAttachment(chosenFile)
    let bananaembed = new MessageEmbed()
      .setTitle("Bonana Machine")
      .setColor(config.color)
      .setDescription("here is your banana!")
      .attachFiles(attachment)
      .setImage(attachment)
      .setFooter(config.footer)
    message.channel.send(bananaembed);
  });

Heres the error:

(node:6072) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat '/home/runner/Gloozzys-Treehouse/banana3.png'

heres the folder structure: folderstructure picture

it thinks the image is in a different directory but I don't know why, if anyone could help me solve this that would be great.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This line return the list of file in the folder ./fun/bananas/

  fs.readdir('./fun/bananas/', (err, files) => {

Reading the file from that folder you have to add the folder path to the MessageAttahment

const attachment  = new MessageAttachment(chosenFile) 

Convert to this.

const attachment = new MessageAttachment('./fun/bananas/' + chosenFile)
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