discord.js question. how do i make the bot give a different response to one command?

Viewed 198

So I have the command -joke I want the bot to give a random response to that command. How do I add it to this code:

module.exports = {
name: 'joke',
description: "tells a joke",
execute(message, args) {
   message.channel.send('Why do we tell actors to break a leg? Because every 
   play has a cast');
 }
}
2 Answers

Use an array to store your jokes.

Assuming your array is called jokes:

const getJoke = () => {
 return jokes[Math.floor(Math.random() * jokes.length))];
}

This should then get you a random one from your array. Just send the output of that function to your user

Create an array with jokes:

const jokes = ["Joke1", "Joke2", "Joke3"];

and get a joke from the array at a random index:

const randJoke = jokes[Math.floor(Math.random() * jokes.length)];

then send the random joke in the current text channel:

message.channel.send(randJoke);
Related