抛硬币命令只给出 'tails' 和 'you loose'
Coin-flip command gives only 'tails' and 'you loose'
我尝试制作一个掷硬币命令,用户键入 .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]}
未定义,尽管我输入了 heads
、h
或 head
; ${coinz}
每次都是反面。
似乎决定以太币的输赢,你将你的选择变量与coinz进行比较。但是,对于您的代码,"choice" 只能是 "h" 或 "t",而 "coinz" 将是 "heads" 或 "tails"。这个比较总是 return false,告诉你这是一个失败 by @Gruntzy
回答收集我的原创评论。
关于100%丢失率:
要决定是赢还是输,您可以将您的选择变量与 coinz 进行比较。但是,对于您的代码,"choice" 只能是 "h" 或 "t" ,而 "coinz" 将是 "heads" 或 "tails"。这个比较总是return假,告诉你输了。
关于未定义的 $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.");
}
}
});
我尝试制作一个掷硬币命令,用户键入 .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]}
未定义,尽管我输入了 heads
、h
或 head
; ${coinz}
每次都是反面。
似乎决定以太币的输赢,你将你的选择变量与coinz进行比较。但是,对于您的代码,"choice" 只能是 "h" 或 "t",而 "coinz" 将是 "heads" 或 "tails"。这个比较总是 return false,告诉你这是一个失败 by @Gruntzy
回答收集我的原创评论。
关于100%丢失率: 要决定是赢还是输,您可以将您的选择变量与 coinz 进行比较。但是,对于您的代码,"choice" 只能是 "h" 或 "t" ,而 "coinz" 将是 "heads" 或 "tails"。这个比较总是return假,告诉你输了。
关于未定义的 $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.");
}
}
});