我的代码的 let 部分产生了一个错误,我通过将 client.login 移动到底部来弄清楚为什么机器人无法启动新错误包括它只是发送垃圾邮件无效的邮政编码.请遵循以下格式:_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
});
<小时>
您需要检查邮件是否由机器人发送,以便它忽略他们发送的所有邮件,包括无效邮政编码"邮件:
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
});
否则,无效邮政编码"消息将触发机器人发送另一个无效邮政编码"消息,因为无效邮政编码"显然不是有效的邮政编码.
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) 因为 isNaN 将其输入强制转换为一个数字,然后检查它是否为 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
`);
}
});
}
}
})
<小时>
if (message.content.startsWith("_weather") && !message.author.bot)
这篇关于获取 SyntaxError:词法声明不能出现在单语句上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
discord.js v12:我如何等待 DM 频道中的消息?discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 频道中的消息?)
如何让我的机器人提及发出该机器人命令的人how to make my bot mention the person who gave that bot command(如何让我的机器人提及发出该机器人命令的人)
如何修复必须使用导入来加载 ES 模块 discord.jsHow to fix Must use import to load ES Module discord.js(如何修复必须使用导入来加载 ES 模块 discord.js)
如何列出来自特定服务器的所有成员?How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
Discord bot:修复“找不到 FFMPEG"Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
使用 discord.js 加入 discord 服务器时的欢迎消息Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)