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

284
Views
Discord.js | How do I make my bot send a random image?

I have tried a bunch of things that I've seen and none of them seem to be working for me. I am using the thing where when you do a command it activates a file in the commands folder, and most of the other scripts dont use that.

1

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Without seeing any code, it is quite difficult to point you in the right direction. It would help more to define the issue you are having (actual errors and/or functionality) along with seeing the code you are using. The solution also depends on where the images are coming from.

Assuming your images are in a local folder, here is a basic example:

if (message.content === '!random') {
    // get a random index between 0 and length
    const idx = (len) => Math.floor(Math.random() * (len));

    // get all the files in the directory
    const files = fs.readdirSync('/images/');

    // get file from directory
    const myImage = files[idx(files.length)];
    
    // send image to channel
    message.channel.send({ files: [`/images/${myImage}`] });
}

Assuming images are from https://picsum.photos/, here is an basic example:

if (message.content === '!random') {
    const seed = Math.floor(Math.random() * 1000);
    const h = Math.floor(Math.random() * 400) + 100;
    const w = Math.floor(Math.random() * 400) + 100;
    
    // create embed with random image
    const embed = new MessageEmbed().setImage(`https://picsum.photos/seed/${seed}/${h}/${w}`);
    
    // send embed
    message.channel.send({ embeds: [embed] });

    // delete command
    message.delete();
}
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!