【问题标题】:Coin-flip command gives only 'tails' and 'you loose'硬币翻转命令只给出“尾巴”和“你松了”
【发布时间】:2019-07-16 12:12:58
【问题描述】:

我尝试发出一个掷硬币命令,用户输入.cf heads,机器人会向他显示他的答案、结果以及他们是赢还是输。

我尝试使用args,但没有它,但它不起作用;我的代码有错误:

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`);
    };
  };
});

代码有效,但它让我 100% 失败,有时${args[1]} 未定义,尽管我输入了headshhead${coinz} 每次都是尾巴。

【问题讨论】:

  • 似乎决定了以太是赢还是输,你将choice变量与coinz进行比较。但是,使用您的代码,“choice”只能是“h”或“t”,而“coinz”将是“heads”或“tails”。这种比较总是会返回 false,告诉你这是一个损失
  • 未定义的 args[1] 来自您的 args.shift()call。移动一个数组会从这个数组中删除第一个元素,所以一旦你提取了“cmd”变量,你的参数现在存储在 args[0] 中。
  • @Gruntzy 因为这是一个答案,您可以这样发布吗?这样作者可以接受它并关闭问题;)
  • @Gruntzy 所以你的意思是我应该删除 `arts. Shift() 并用 h 和 t 替换 coinz??

标签: javascript node.js discord.js coin-flipping


【解决方案1】:

回答收集我原来的 cmets。

关于100%的损失率: 要决定是赢还是输,您可以将您的选择变量与 coinz 进行比较。但是,使用您的代码,"choice" 只能是 "h""t",而 "coinz" 将是 “正面”“反面”。这种比较总是会返回 false,告诉你这是一个损失。

关于未定义的 $args[1] : args[1] undefined 来自您的args.shift() 调用。移动一个数组会从这个数组中删除第一个元素,所以一旦你提取了“cmd”变量,你的参数现在存储在 args[0] 中。 我在这里解决此问题的方法是将cmd var 存储为args[0],将choice var 存储为args[1],不进行移位。 另请注意,您仍然可能会遇到错误,因为您的测试:

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

没有括号,这意味着无论条件是否通过,以下行都将运行,这意味着您将尝试访问 args[1] 即使它不存在。您应该将所有以下代码包装在{ } 中,因为它取决于args[1] 变量:

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.");
    }
}


});

【讨论】:

    【解决方案2】:

    似乎决定以太是赢还是输,你将你的选择变量与coinz进行比较。但是,使用您的代码,“选择”只能是“h”或“t”,而“coinz”将是“正面”或“反面”。这种比较总是会返回 false,告诉你这是 @Gruntzy 的损失

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多