The title explains it I guess. I have no code written down and it would be nice if someone explains the steps to the code. I want it to send a random link for a wallpaper from a website each time the command 'a wallpaper' is used. Any helpers?
Asked
Active
Viewed 1.0k times
2 Answers
1
You would have to create an Array
containing the responses you want to send. Then, you can use Math.random
to get a random number between 0 and 1 (1 inclusive) and Math.floor
to get the index ranging from 0 to arrayLength - 1.
const Responses = [
"image 1",
"image 2",
"image 3",
"image 4",
"image 5"
];
const Response = Math.floor(Math.random() * Responses.length);
console.log(Responses[Response])
client.on("message", message => {
if (message.author.bot) return false;
if (!message.guild) return false;
if (message.content.indexOf(Prefix) !== 0) return false;
const arguments = message.content.slice(Prefix.length).split(/ +/g); // Splitting the message.content into an Array with the arguments.
// Input --> !test hello
// Output --> ["test", "hello"]
const command = arguments.shift().toLowerCase();
// Removing the first element from arguments since it's the command, and storing it in a variable.
if (command == "random") {
const Response = Math.floor(Math.random() * Responses.length);
message.channel.send(`Here is your random response: ${Responses[Response]}`);
};
});

Jakye
- 6,440
- 3
- 19
- 38
-
but how would the bot know which command to answer to. thats the thing thats been confusing me in every article i see – NumberedBore7 Sep 15 '20 at 02:49
-
Isn't that only one response though? Also I'm so sorry. Excuse my ignorance in this topic – NumberedBore7 Sep 15 '20 at 03:08
-
No. A response is generated each time the command is ran, because we are randomizing an item from the `Responses` array in the `Client#message` event. – Jakye Sep 15 '20 at 07:07
-
Thank you so much! It works. I'll be marking the answer – NumberedBore7 Sep 15 '20 at 10:55
-
Do you have any idea why it keeps selecting the same images every time? – NumberedBore7 Sep 17 '20 at 03:02
-1
client.on('message', (message) => {
if (message.content.startsWith('!wallpaper')) {
// Here you can choose between creating your array with your own links or use a api that will generate it for you. For now i'll use an array
const wallpapers = [
'https://cdn.discordapp.com/attachments/726436777283289088/734088186711769088/images.png',
'https://cdn.discordapp.com/attachments/726436777283289088/734088144730718258/images.png',
'https://cdn.discordapp.com/attachments/726436777283289088/734087421918183484/images.png',
// You can add as many as you want
];
// Here we will create a random number. Math.random only creates a randomly generated number between 0 and 1.
const response = wallpapers[Math.floor(Math.random() * wallpapers.length)];
message.reply(response);
}
});
If you'd like, you can see more information about randomization in w3school / Randomization

Lioness100
- 8,260
- 6
- 18
- 49

87message.author.username7
- 232
- 2
- 10