Alroy
Alroy

Reputation: 11

DiscordJS - Embedding a random image from an array

I have a command for my DiscordJS bot that has an array of Imgur links. I want it to select a single link randomly and embed that in a message to the channel. I think I am close. How do I call it from the array?

if (command == "dude") return dude(args, message); 

function dude(args, message) {
  if (args.length > 0)
    return message.channel.send(You are not using this command correctly.);
  const embed = new Discord.RichEmbed();
  var dudes = ["imgururl1", "imgururl2", "imgururl3", "imgururl4" ];
  var dude = Math.floor(Math.random() * dudes.length);
  embed.setImage([dude]);
  message.channel.send(embed);
};

Upvotes: 1

Views: 913

Answers (2)

Super Phantom User
Super Phantom User

Reputation: 85

It looks good, just make sure, as Kaynn said, to call the dude variable from the dudes array:

dudes[dude];

Or, you can define dude with it altogether:

var dude = dudes[Math.floor(Math.random() * dudes.length)];

Also, make sure to send the message within double quotes to make it a string, otherwise, the system is going to give a syntax error.

Upvotes: 0

WQYeo
WQYeo

Reputation: 4056

You just needed to access the array with the given index.

embed.setImage(dudes[dude]);

Upvotes: 3

Related Questions