discord.js bot random images

Viewed 31

I want to create a bot that picks a random image from an array of links, but I don't know what's wrong with it, here's the code and error

const arg = message.content.slice(prefix.lenght).split(/ +/);
const command = arg.shift().toLowerCase();

if(command === 'jtoh'){
  
  const rando_imgs = [
    'https://media.giphy.com/media/CZpro4AZHs436/giphy.gif',
    'https://media.giphy.com/media/CZpro4AZHs436/giphy2.gif',
    'https://media.giphy.com/media/CZpro4AZHs436/giphy3.gif',
    ]
    message.channel.send( {
      file: rando_imgs[Math.floor(Math.random() * rando_imgs.length)]
  });
}
1 Answers

The error is that the send() method accepts an object with a files property (not file). This should be an array where you can add your random image. The following will work for you:

if (command === 'jtoh') {
  const rando_imgs = [
    'https://media.giphy.com/media/CZpro4AZHs436/giphy.gif',
    'https://media.giphy.com/media/CZpro4AZHs436/giphy2.gif',
    'https://media.giphy.com/media/CZpro4AZHs436/giphy3.gif',
  ];
  message.channel.send({
    files: [rando_imgs[Math.floor(Math.random() * rando_imgs.length)]],
  });
}

If you want to display the message, you could just use the content property instead. In this case, you don't have to send an array.

message.channel.send({
  content: rando_imgs[Math.floor(Math.random() * rando_imgs.length)],
});

Also, there is a typo; prefix.lenght should be prefix.length.

const arg = message.content.slice(prefix.length).split(/ +/);
Related