-1

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.

Federico Grandi
  • 6,048
  • 4
  • 23
  • 41
  • 2
    it seems like to decide ether it's a win or a loss, you compare your `choice` variable with `coinz`. However, with your code, "choice" can only be "h" or "t", while "coinz" will be "heads" or "tails". This comparison will always return false, telling you it's a loss – Gruntzy Jul 16 '19 at 15:02
  • 1
    The args[1] undefined comes from your `args.shift()`call. Shifting an array removes the 1st element from this array, so once you extracted the 'cmd' variable, your argument is now stored in args[0]. – Gruntzy Jul 16 '19 at 15:20
  • @Gruntzy since that is an answer can you post it as such? This way the author can accept it and close the question ;) – Federico Grandi Jul 16 '19 at 17:01
  • @Gruntzy so you mean I should remove `arts. Shift() and replace coinz with h and t?? – hokage minato Jul 16 '19 at 19:47

2 Answers2

1

Answer gathering my original comments.

Regarding the 100% loss rate : To decide either it's a win or a loss, you compare your choice variable with coinz. However, with your code, "choice" can only be "h" or "t", while "coinz" will be "heads" or "tails". This comparison will always return false, telling you it's a loss.

Regarding the undefined $args[1] : The args[1] undefined comes from your args.shift() call. Shifting an array removes the 1st element from this array, so once you extracted the 'cmd' variable, your argument is now stored in args[0]. My way to fix this here would be to store a cmd var as args[0] and a choice var as args[1], with no shifting. Please also note that you still may encounter an error, because your test :

if (args[1] != undefined)
  args[1] = args[1].toLowerCase();

There are no brackets, meaning the following lines will be run wether the condition passes or not, meaning you will try to access args[1] even if it doesn't exist. You should wrap all your following code in { } because it depends on the args[1] variable :

client.on('message', message => {
const prefix = '.';
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g)
const cmd = args[0].toLowerCase();

console.log(`CMD : ${cmd}`);
// Little optimization here, check for the command before doing anything else.
if (cmd === 'cf' || cmd === 'coin' || cmd === 'flip' || cmd === 'coinflip') {

    if (args[1] != undefined) {
        let choice = args[1].toLowerCase();
        console.log(`choice : ${choice}`);

        if (choice == 'heads' || choice == 'h' || choice == 'head') {
            choice = 'heads';
        }

        else if (choice == 'tails' || choice == 't' || choice == 'tail') {
            choice = 'tails';
        }


        var coins = [
            "heads",
            "tails"
        ];
        coinz = coins[Math.floor(Math.random() * coins.length)];

        if (choice != coinz) {
            message.channel.send(`Your bet: \`${choice}\`,
            outcome: \`${coinz}\` you lose`);
        } else {
            message.channel.send(`Your bet: \`${choice}\`,
                outcome: \`${coinz}\` you win`);
        };
    }
    else {
        message.reply("please choose *heads* or *tails* before the coin flip.");
    }
}


});
Gruntzy
  • 353
  • 1
  • 7
0

it seems like to decide ether it's a win or a loss, you compare your choice variable with coinz. However, with your code, "choice" can only be "h" or "t", while "coinz" will be "heads" or "tails". This comparison will always return false, telling you it's a loss by @Gruntzy