Answer the question
In order to leave comments, you need to log in
How to use a discord bot to send a photo from a specific folder to a chat?
The bottom line is this:
My discord bot should, on a command in the chat, send a randomly selected photo there. All photos that the bot can send are in one folder.
I was able to get the result, but for this I have to specify the full path from the disk to the photo itself for each individual photo, and given that there are about 200 of these photos, it is not very convenient to write 200 lines of the same code.
Here is a part of my code:
Is it possible for the bot to write down the path only to the folder with photos that he himself will find there and choose a random one?
client.on('message', message => {
if (message.author.bot) return;
if (message.content.startsWith('=мем')) {
let images = [
{ files: ["./Images/e633993d7c13dcbe83f4e7969cc06e63.jpg"] },
];
let randomiez = Math.floor(Math.random() * images.length)
let randomImage = images[randomiez];
message.channel.send(randomImage);
}
});
Answer the question
In order to leave comments, you need to log in
Maybe something like this
const fs = require('fs');
const IMG_DIR_PATH = './images/'
const imgArr = fs.readdirSync(IMG_DIR_PATH);
function randomImage() {
let randomIndex = Math.floor(Math.random() * imgArr.length);
let imgFileName = imgArr[randomIndex];
return IMG_DIR_PATH + imgFileName;
};
client.on('message', message => {
let images = [
{ files: [randomImage()] },
];
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question