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.
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)