chamichi
chamichi

Reputation: 13

How would I make a bot react to messages, but randomly?

I'm making a private discord bot for my server. I would like to have it randomly react to messages, but I can't seem to figure out the code to do so. Everytime I try, instead of reacting to different messages, he reacts to every message sent.

I've tried using math.random()... but clearly that didn't work, lol. I've just started coding last week ish, so I'm pretty new when it comes to this stuff. Here's my code:

client.on("message", (message) => {
const PrettyPlease = client.emojis.find(emoji => emoji.name === "PrettyPlease");{
number = 5;
randomNumber = Math.floor (Math.random() * (number - 1 + 1)) + 1; 
message.react (PrettyPlease);
}

 })

Upvotes: 0

Views: 1097

Answers (1)

Stankeneddy774
Stankeneddy774

Reputation: 101

Math.random() will return a random number between 0 (inclusive) and 1 (exclusive). Therefore, you can generate a random value and compare it.

if (Math.random() < .5) console.log('React to the message.');

This example will have a 50% chance of printing the message.

< .25 would produce a 25% chance,
< .75 would produce a 75% chance,
...and so on.

Upvotes: 1

Related Questions