問題描述
我的代碼的 let 部分產(chǎn)生了一個錯誤,我通過將 client.login 移動到底部來弄清楚為什么機(jī)器人無法啟動新錯誤包括它只是發(fā)送垃圾郵件無效的郵政編碼.請遵循以下格式:_weather <#####>" 即使你輸入郵政編碼
The let part of my code is producing an error I figured out why the bot wouldn't start by moving the client.login to the bottom new error includes it just spamming "Invalid Zip Code. Please follow the format: _weather <#####>" even if you put in the zipcode
client.on("message", (message) => {
if (message.content.includes("_weather") && message.author.bot === false)
let zipCode = message.content.split(" ")[1];
if (zipCode === undefined || zipCode.length != 5 || parseInt(zipCode) === NaN) {
message.channel.send("`Invalid Zip Code. Please follow the format: _weather <#####>`")
.catch(console.error);
return;
} else {
fetch(`https://openweathermap.org/data/2.5/weather?zip=${zipCode},us&appid=439d4b804bc8187953eb36d2a8c26a02`)
.then(response => {
return response.json();
})
.then(parsedWeather => {
if (parsedWeather.cod === '404') {
message.channel.send("`This zip code does not exist or there is no information avaliable.`");
} else {
message.channel.send(`
The Current Weather
Location: ${parsedWeather.name}, ${parsedWeather.sys.country}
Forecast: ${parsedWeather.weather[0].main}
Current Temperature: ${(Math.round(((parsedWeather.main.temp - 273.15) * 9 / 5 + 32)))}° F
High Temperature: ${(Math.round(((parsedWeather.main.temp_max - 273.15) * 9 / 5 + 32)))}° F
Low Temperature: ${(Math.round(((parsedWeather.main.temp_min - 273.15) * 9 / 5 + 32)))}° F
`);
}
});
}
});
client.login('token');
推薦答案
你不能在像 if 這樣的語句之后使用詞法聲明(
、const
和 let
)else
、for
等沒有塊 ({}
).改用這個:
You can't use lexical declarations (const
and let
) after statements like if
, else
, for
etc. without a block ({}
). Use this instead:
client.on("message", (message) => {
// declares the zipCode up here first
let zipCode
if (message.content.includes("_weather") && message.author.bot === false)
zipCode = message.content.split(" ")[1];
// rest of code
});
<小時>
編輯第二個問題
您需要檢查郵件是否由機(jī)器人發(fā)送,以便它忽略他們發(fā)送的所有郵件,包括無效郵政編碼"郵件:
Edit for 2nd question
You need to check if the message was sent by a bot so that it will ignore all messages sent by them, including the 'Invalid Zip Code' message:
client.on("message", (message) => {
if (!message.author.bot) return;
// rest of code
});
否則,無效郵政編碼"消息將觸發(fā)機(jī)器人發(fā)送另一個無效郵政編碼"消息,因?yàn)闊o效郵政編碼"顯然不是有效的郵政編碼.
Without that, the 'Invalid Zip Code' message would trigger the bot to send another 'Invalid Zip Code' message as 'Invalid Zip Code' is obviously not a valid zip code.
另外,將 parseInt(zipCode) === NaN
更改為 Number.isNaN(parseInt(zipCode))
.NaN === NaN
在 JS 中由于某種原因是 false
,所以你需要使用 Number.isNaN
.你也可以只做 isNaN(zipCode)
因?yàn)?isNaN
將其輸入強(qiáng)制轉(zhuǎn)換為一個數(shù)字,然后檢查它是否為 NaN
.
Also, change parseInt(zipCode) === NaN
to Number.isNaN(parseInt(zipCode))
. NaN === NaN
is false
for some reason in JS, so you need to use Number.isNaN
. You could also just do isNaN(zipCode)
because isNaN
coerces its input to a number and then checks if it's NaN
.
console.log(`0 === NaN: ${0 === NaN}`)
console.log(`'abc' === NaN: ${'abc' === NaN}`)
console.log(`NaN === NaN: ${NaN === NaN}`)
console.log('')
console.log(`isNaN(0): ${isNaN(0)}`)
console.log(`isNaN('abc'): ${isNaN('abc')}`)
console.log(`isNaN(NaN): ${isNaN(NaN)}`)
console.log('')
console.log(`Number.isNaN(0): ${Number.isNaN(0)}`)
console.log(`Number.isNaN('abc'): ${Number.isNaN('abc')}`)
console.log(`Number.isNaN(NaN): ${Number.isNaN(NaN)}`)
試試這個代碼:
client.on("message", (message) => {
if (message.content.includes("_weather") && !message.author.bot) {
let zipCode = message.content.split(" ")[1];
if (zipCode === undefined || zipCode.length != 5 || Number.isNaN(parseInt(zipCode))) {
message.channel.send("`Invalid Zip Code. Please follow the format: _weather <#####>`")
.catch(console.error);
return;
} else {
fetch(`https://openweathermap.org/data/2.5/weather?zip=${zipCode},us&appid=439d4b804bc8187953eb36d2a8c26a02`)
.then(response => {
return response.json();
})
.then(parsedWeather => {
if (parsedWeather.cod === '404') {
message.channel.send("`This zip code does not exist or there is no information avaliable.`");
} else {
message.channel.send(`
The Current Weather
Location: ${parsedWeather.name}, ${parsedWeather.sys.country}
Forecast: ${parsedWeather.weather[0].main}
Current Temperature: ${(Math.round(((parsedWeather.main.temp - 273.15) * 9 / 5 + 32)))}° F
High Temperature: ${(Math.round(((parsedWeather.main.temp_max - 273.15) * 9 / 5 + 32)))}° F
Low Temperature: ${(Math.round(((parsedWeather.main.temp_min - 273.15) * 9 / 5 + 32)))}° F
`);
}
});
}
}
})
<小時>
編輯 3
if (message.content.startsWith("_weather") && !message.author.bot)
這篇關(guān)于獲取 SyntaxError:詞法聲明不能出現(xiàn)在單語句上下文中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!