Pick random image from folder in NodeJS?
Go to solution
Solved by jincio,
You could use the native library 'fs'. and use a method of that library that is readdirSync()
var fs = require('fs'); var files = fs.readdirSync('/path/to/dir/') /* now files is an Array of the name of the files in the folder and you can pick a random name inside of that array */ let chosenFile = files[Math.floor(Math.random() * files.length)]
the discord part seems correct, only that you need to change the path to the image to
'/path/to/image/' + chosenFile
P.S. you could use the variable '__dirname' which is the path of the current module so for example if your .js is in '/home/user/Project/discord-bot/app.js' and you need the image in '/home/user/Project/discord-bot/images/image.jpg' you can just write __dirname + '/images/image.jpg'

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now