【发布时间】:2020-09-03 19:56:40
【问题描述】:
我的代码目前将检查输入是否有效,我正在使用 or 运算符来执行此操作。
if (tosell !== 'xmr' || tosell !== 'eth' || tosell !== 'btc') {
message.channel.send('Unknown currency. Valid currencies are: xmr, eth, btc. Example: $sell xmr')
} else {
message.channel.send('How much '+tosell+' would you like to sell?')
const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { time: 5000 });
collector.on('collect', async message => {
amount = parseInt(message.content)
if (amount === 'all') {
realamount = score.xmr
} else if (amount != 'all') {
console.log(score.xmr)
realamount = amount
}
if (tosell === 'xmr') {
if (userscore.xmr >= amount) {
var cg = await fetch('https://api.coingecko.com/api/v3/coins/monero?tickers=true&market_data=true&community_data=false&developer_data=false&sparkline=false').then(response => response.json());;
const xmrprice = cg.market_data.current_price.usd
const toAdd = realamount*xmrprice
userscore.xmr -= amount
userscore.usd += toAdd
const embed = new Discord.MessageEmbed()
.setColor('#7FDB7F')
.setTitle('You sold '+amount+'XMR for $'+toAdd)
.setAuthor('Successful Sell', 'https://cdn.discordapp.com/emojis/710590499991322714.png?v=1')
message.channel.send(embed)
client.setScore.run(userscore);
} else {
message.channel.send("You don't have enough Monero to sell!")
}
}
})
}
但是当它这样做时,由于某种原因它无法识别/工作,即使我输入值 xmr,它也会执行我定义的“else”。如果有人知道我在使用 or 运算符时做错了什么,请告诉我
【问题讨论】:
-
你的陈述永远不会是假的 - 那么它是如何做其他部分的呢?
-
编辑它以显示我的完整代码
-
为什么?你的测试永远不会是假的
-
tosell 是用户输入
-
所以?你的测试永远不会是假的——无论
tosell是什么,它都不等于其中至少两个值,因此,条件总是为真——你可能想使用 && 而不是 ||
标签: javascript node.js if-statement discord.js