0

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?

1
  • The last part of the code seems to be missing. There are 13 {s and 11 }s. Commented Jul 22, 2022 at 13:10

2 Answers 2

2

You should use == instead of === in your if (num === "1") statements or remove double quotes around the number—if (num === 1).

That's because the === operator is more strict; it stands for comparing value and type of the value. The Math.random() function returns the value of the number type, so you are comparing number to string:

console.log(3 == "3"); // Will return true
console.log(3 === "3"); // Will return false

More information is on Expressions and operators.

Sign up to request clarification or add additional context in comments.

3 Comments

ahh. Thanks. I'll keep that in mind when im working with variables.
Do you know what to do about the case insensitivity though? when i say !Guessing game, it doesn't work so i need to make it so it converts it all to lower case.
@Datonebassdude use : YourVariable.toLowerCase(); :)
0

You could also have made your code a lot shorter using:

const numbers = ['1️⃣','2️⃣','3️⃣'];
let randNum = numbers[Math.floor(Math.random() * numbers.length)];
// Output: '1️⃣' or '2️⃣' or '3️⃣' :)

1 Comment

@Datonebassdude No problem :) Please consider upvoting answers that where helpful to you using the arrows next to them :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.