-5

So I'm making a discord bot in node.js and I'm using discord.js library for this. I want when someone types [p]catgif it sends a random image from a local folder. I'm trying to make it so it picks a random number from 1 to 100 or something like that and then it looks in a folder and selects that image with that number like if the number was 1 it would look for 1.png in that folder. How do i get the number it has picked to be the image i have learned how to generate numbers but not how to combine it with sending an image/attachments i know both but now how to combine the two

I've looked on discord.js docs but mainly only found how to attach one image or website. I've tried the code below to see if that work but it had little success only lots of errors. I've look on here but only found one that was similar the only problem with that one is its in python and i'm using node.

const {token, prefix} = require('./setting.json');

client.on('message', message => {
    if (message.content === prefix + 'catgid') {
        const attachment = new Attachment(f'./${random 1-10}.png');
        message.channel.send(`${message.author},`, attachment);
    }
});
  • 1
    Are you asking how to come up with a random number between 1 and 100 in NodeJS? – James Aug 05 '19 at 23:00
  • 1
    Possible duplicate of [Generate random number between two numbers in JavaScript](https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript) – André Ekeberg Aug 05 '19 at 23:04
  • i want to generate a number then that number it looks for in a fold and then it once its found it then it sends the image with that number – Scott Pierre Zangenberg Aug 05 '19 at 23:06

1 Answers1

-1

I think you should do this by first generating a random number (see: Generate random number between two numbers in JavaScript ) and then save that as the variable num. Then, replace the ${random 1-10} with ${num} and switch the ' to `. I hope this helps. Also, I'm not sure why there is a random f before the file name but I think you should remove that.

yogurtsyum
  • 348
  • 1
  • 2
  • 17