Javascript 投币命令仅给出';尾巴';和';你松开了';
我试着做一个掷硬币的命令,用户键入Javascript 投币命令仅给出';尾巴';和';你松开了';,javascript,node.js,discord.js,coin-flipping,Javascript,Node.js,Discord.js,Coin Flipping,我试着做一个掷硬币的命令,用户键入.cf heads,机器人向他显示他的答案、结果以及他们是赢了还是输了 我试着使用args,但没有它,但它不起作用;我的代码中有一个错误: bot.on('message', message => { const prefix = '.'; if (!message.content.startsWith(prefix)) return; const args = message.content.slice(prefix.length).trim
.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”。这个比较总是会返回false,告诉你@Gruntzy答案收集我的原始评论是一个损失
关于100%的损失率:
要决定是赢还是输,您可以将您的选择变量与coinz进行比较。但是,在代码中,“choice”只能是“h”或“t”,而“coinz”将是“heads”或“tails”。这种比较总是会返回false,告诉你这是一种损失
关于未定义的$args[1]:
未定义的args[1]来自您的
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.");
}
}
});
你将你的
选择
变量与coinz
进行比较,似乎可以决定这是赢还是输。但是,在代码中,“choice”只能是“h”或“t”,而“coinz”将是“heads”或“tails”。此比较将始终返回false,告诉您这是一个损失args[1]未定义来自您的args.shift()
调用。移动数组将删除此数组中的第一个元素,因此一旦提取了“cmd”变量,您的参数现在将存储在args[0]中。@Gruntzy既然这是一个答案,您可以这样发布它吗?这样,作者就可以接受它并结束问题;)@Gruntzy所以你的意思是我应该删除'arts'。Shift()并将coinz替换为h和t??