Reputation: 31
Making a discord bot. Getting "you win" on not only a 6 roll, but 2 and 4 as well. I know this is not the best way to make it. It doesn't seem to care if random == 'insert string here' or random == 'insert int here'.
//Dice Roll Game
bot.on('message', (message) =>{
let diceNum = ['1','2','3','4','5','6'];
let random = diceNum[Math.floor(Math.random() * diceNum.length)];
if(message.content == '!roll') {
message.reply('You rolled a' + ' ' + random + '!');
}
if(random == 6){
message.reply('You win!');
}
});
Upvotes: 1
Views: 392
Reputation: 376
I see the main problems with your code is that:
You did not put your all of your dice-related code into the
if
-block checking if the message is theroll
command.
- This causes the bot to reply when the number "rolled" is 6 even when the command is not invoked.
You did not check whether the message came from a bot.
- It would reply multiple times since you did not check whether the message came from your bot.
Your code would look like this once you fixed all your errors:
//Dice Roll Game
bot.on('message', message => { // If theres only one parameter, you can omit brackets
// Bot Check
if(message.author.bot)return;
// Even with useless parameters to the command, it will still run
if(message.content.startsWith('!roll')) {
// Arrays are not needed
// Gives a random int from 1 - 6, (~~) floors an integer
let random = ~~(Math.random() * 6) + 1;
message.reply(`You rolled a ${ random }!`); // ES6 Template Strings
// Please use strict equality signs to prevent bugs from appearing in your code
if(random === 6){
message.reply('You win!');
}
}
});
Side Note: If you do not want a mention prepended to your bot message, please use message.channel.send
instead of message.reply
.
Upvotes: 2