Search code examples
javascriptbotsdiscorddice

How to make a command on a discord bot to randomly generate a quote?


I'm coding my own discord bot and in a nutshell I need some help with the code. I would need my bot to pick a random response (1/10 for example) when I use a command like !roll or !dice, and once used, a user cool down of 6 days to be added (i.e the command can't be used by said user for 6 days afterwards, and would respond with the days, hours and minutes)

I'm pretty new to code, but I figured out some

const Discord = require ("discord.js")
const Bot = new Discord.Client();
const token = "my token"
Bot.login('my token');

Bot.on('message' , function (message){
if(message.content == '!roll')
var roll =(Math.floor(Math.random()*200)+1);
   if (roll = 1 )
    {message.reply('Wowza!');
}
        else {message.reply('lol!');
    }

whenever i roll, the bot displays either one of the outputs, then the other, and then spams the two till it is shut off.


Solution

  • Don't use a single = in a comparison statement such as an if. A single equals is an assignment operator which means when you write roll = 1, you are setting roll to 1 instead of checking if the value it holds is 1.

    Always use == or === when doing comparison statements. So, you would want to use roll == 1 or roll === 1.

    Here's a good article about double equals vs triple equals in JavaScript

    if (roll == 1 ) {
        message.reply('Wowza!');
    }
    else {
        message.reply('lol!');
    }
    

    Also, after looking further at your code, you have some more issues. You want to wrap the first if in a set of curly braces since it should encompass the following lines of code.

    Bot.on('message' , function (message){
        if (message.content == '!roll') 
        {
            var roll =(Math.floor(Math.random()*200)+1);
            if (roll == 1)
            {
                message.reply('Wowza!');
            }
            else 
            {
                message.reply('lol!');
            }
        }
    }