I'm trying to make a guessing game with my Discord bot where when I type "!guessing game". It will react with three emojis: 1️⃣, 2️⃣, and 3️⃣. Then it randomly chooses one of those emojis and assigns it to the variable, "answer". And if I click the emoji that corresponds with the one that is set to the "answer" variable then it will say something like "Congratulations! You guessed the correct number!", and if not, it would say "Aww! Better luck next time!" or something.
This is my code:
client.on('message', msg => {
if (msg.content === "!guessing game") {
const filter = (reaction, user) => {
return ['1️⃣', '2️⃣', '3️⃣'].includes(reaction.emoji.name) && user.id === msg.author.id;
};
msg.react('1️⃣');
msg.react('2️⃣');
msg.react('3️⃣');
msg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
var num = Math.floor(Math.random() * 3) + 1;
if (num === "1") {
var answer = "1️⃣"
}
else {
if (num === "2") {
var answer = "2️⃣"
}
else {
if (num === "3") {
var answer = "3️⃣"
}
else {
console.log("Random num failed")
}
}
}
if (reaction.emoji.name === answer) {
msg.reply('You did it!');
}
else {
msg.reply('Better luck next time!');
}
})
Naturally, this is what my console said: "Random num failed" I have a feeling that it would be a lot more efficient to choose a random emoji rather a random number and then converting it to an emoji.
How can I fix this? Could it be made case insensitive?
{s and 11}s.