Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!