Discord bot not responding to command with random messages

Discord Bot Random Response Issue

I’m building a Discord bot that should respond with random messages when users type a specific command. The idea is simple - when someone types “startgame”, the bot should pick one of four different responses and send it back. However, my bot stays silent and doesn’t reply at all.

Here’s my current code:

client.on('messageCreate', (message) => {
    if(message.content === "startgame"){
        var randomNum = Math.random() * (4 - 1) + 1;
        Math.round(randomNum);
        if(randomNum === 1){
            message.channel.send('sword, shield, potion');
        }
        else if(randomNum === 2){
            message.channel.send('magic, sword, gem');
        }
        else if(randomNum === 3){
            message.channel.send('gem, potion, shield');
        }
        else if(randomNum === 4){
            message.channel.send('potion, magic, sword');
        }
    }
})

Can anyone spot what might be wrong with my code? Any help would be appreciated!

you should assign the rounded number back to randomNum, like randomNum = Math.round(randomNum);. also, using Math.random() * 4 + 1 simplifies things a bit. hope that helps!

Skip the Math.round headaches - I’d automate this completely differently. Built something similar last month and moved it to Latenode after my bot kept crashing.

Yeah, your Math.round isn’t saving to the variable, but that’s not the real problem. Every time you want new responses or different logic, you’re editing code and redeploying.

I built a Latenode workflow that catches Discord webhooks, grabs random responses from an array, and shoots them back. Takes 5 minutes to set up and you can change responses through a web interface instead of messing with code.

Just use a random number node connected to a switch that routes different messages. Way cleaner than a pile of if statements, and adding new responses doesn’t require any coding.

Want to get fancy later? Connect it to a database or add user-specific responses based on game history. All drag and drop.

You’re calling Math.round() but not storing the result anywhere. When you write Math.round(randomNum);, it calculates the rounded value then throws it away since you’re not saving it back to randomNum. Your variable still has the original decimal, which won’t match 1, 2, 3, or 4 in your comparisons. Just use Math.floor(Math.random() * 4) + 1 instead - gives you clean integers from 1 to 4 without rounding. I’ve hit this same issue before with random numbers. Floating point comparisons are a pain.

hey, def add a check for bots at the start: if (message.author.bot) return;. also, for your random number part, use randomNum = Math.floor(Math.random() * 4) + 1; to get correct values. should fix your issue!

Math.round() doesn’t change your original variable - it returns a new value that you’re not storing. Your randomNum still has the decimal, so it’ll never equal 1, 2, 3, or 4. You need to capture it: randomNum = Math.round(randomNum); Better option: use Math.floor(Math.random() * 4) + 1 to get integers 1-4 directly without rounding. Also throw if (message.author.bot) return; at the start of your handler so the bot ignores its own messages.