Home > other >  node.js - Is there a way to make a discord bot send random pictures pulled from a file? node.js
node.js - Is there a way to make a discord bot send random pictures pulled from a file? node.js

Time:11-27

So I'm new to Coding and I wanted to make a discord bot, I have an idea of how to do it, but I can't figure out how to do what I want. I was wondering if there is a way to add a bunch of photos a file and have the discord bot be able to pull a a random picture out of it to send it? As well as maybe on that pulls like 5 random ones and sends them?

I have no idea what to do, and I haven't found anything to help with it.

Currently I'm doing one where I have to add every image ID on its own and make really long lines of code for every command.

Any ideas???

CodePudding user response:

You can use Math.random() to pick a random image out of an array of images.

const imageArray = [...];
const randomImage = imageArray[Math.floor(Math.random()*imageArray.length)];

CodePudding user response:

function a(v) {
  Object.defineProperty(v, "rand", {
    get: function() {
      return v[Math.floor(Math.random()*v.length)];
    }
  }); return v.rand;
} console.log(a(["a","b","c"]));

(new edit since my previous answer didnt seem to sit right with some people)

you can store the files in an array and create an object to call the random logic, run the code above to see what im talking about

  • Related