I tried to make a coin flip command where the user types .cf heads
and the bot shows him his answer, the result and whether they won or lost.
I tried using args
and without it but it didn't work; there is an error in my code:
bot.on('message', message => {
const prefix = '.';
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g)
const cmd = args.shift().toLowerCase();
var choice = 'h';
if (args[1] != undefined)
args[1] = args[1].toLowerCase();
if (args[1] == 'heads' || args[1] == 'h' || args[1] == 'head')
choice = 'h';
else if (args[1] == 'tails' || args[1] == 't' || args[1] == 'tail')
choice = 't';
if (cmd === 'cf' || cmd === 'coin' || cmd === 'flip' || cmd ===
'coinflip') {
var coins = [
"heads",
"tails"
];
coinz = coins[Math.floor(Math.random() * coins.length)];
if (choice != coinz) {
message.channel.send(`Your bet: \`${args[1]}\`,
outcome: \`${coinz}\` you lose`);
} else {
message.channel.send(`Your bet: \`${args[1]}\`,
outcome: \`${coinz}\` you win`);
};
};
});
The code worked but it gives me 100% lose and sometimes the ${args[1]}
is undefined although I typed heads
, h
or head
; ${coinz}
is tails every time.