-
Problem: I am trying to make a discord bot in VSCode with discord.js and I want it to respond to me if I say something but for it to not always be the same as the last time, here is an example, me: Hello or Hi or Sup or Yo (case insensitive) bot: Sup or Hello or Hi or Yo (case insensitive) this is my code so far:
any help would be greatly appreciated edit: without having to restart the program every time I want a different answer |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Your regex test doesn't cater for all possible inputs. You could just check if the message content is an element of the array you have initialised rather than using regex (and use |
Beta Was this translation helpful? Give feedback.
-
You are generating your random number once, and storing it as a constant. This never changes, so it will always refer to the same response. Generate a new random number each message event. |
Beta Was this translation helpful? Give feedback.
-
call the random function inside let repliesHi = ["Hi", "Hello", "Yo", "Sup"];
client.on("message", message => {
if (message.author.bot) return;
if (/\bhi\b/i.test(message.content)) {
let randomHi = Math.floor(Math.random() * repliesHi.length);
message.channel.send(repliesHi[randomHi]);
}
}); this way, each time the callback function for the |
Beta Was this translation helpful? Give feedback.
call the random function inside
client.on
this way, each time the callback function for the
message
event is called, a new random number would be generated, instead of having it be generated one time at the beginning of the file