Skip to content
Snippets Groups Projects
Commit 288fe060 authored by mcmlxxix's avatar mcmlxxix
Browse files

check api result for success (200) response. use var instead of const for...

check api result for success (200) response. use var instead of const for openweathermap instance (to accomodate ircbot "reload" command)
parent b75a7f57
No related branches found
No related tags found
No related merge requests found
......@@ -12,24 +12,26 @@ Bot_Commands["WEATHER"].command = function (target, onick, ouh, srv, lvl, cmd) {
try {
const params = get_params(cmd, onick, srv);
if (params) {
const res = owm.call_api('weather', params);
var str = '\0010\1n\1rWeather for \1h\1c' + res.name + '\1n\1r: '
+ '\1h\1c' + res.weather[0].main + ' \1n\1r(\1c' + res.weather[0].description + '\1r), '
+ '\1h\1c' + res.clouds.all + '% cloudy\1n\1r, '
+ 'Current temp: \1h\1c' + temperature_str(res.main.temp) + '\1n\1r, '
+ 'Min temp: \1h\1c' + temperature_str(res.main.temp_min) + '\1n\1r, '
+ 'Max temp: \1h\1c' + temperature_str(res.main.temp_max) + '\1n\1r, '
+ 'Wind: \1h\1c' + res.wind.speed + ' KM/h ' + owm.wind_direction(res.wind.deg) + '\1n\1r, '
+ 'Humidity: \1h\1c' + res.main.humidity + '%\1n\1r, '
+ 'Pressure: \1h\1c' + res.main.pressure + ' hPa\1n\1r, '
+ 'Sunrise: \1h\1c' + system.timestr(res.sys.sunrise) + '\1n\1r, '
+ 'Sunset: \1h\1c' + system.timestr(res.sys.sunset)
+ ' \1n\1m(\1h\1mProvided by OpenWeatherMap.org\1n\1m)';
srv.o(target, ctrl_a_to_mirc(str));
} else {
}
if (!params) {
throw("error parsing parameters");
}
const res = owm.call_api('weather', params);
if(!res || res.cod != 200) {
throw(JSON.stringify(res));
}
var str = '\0010\1n\1rWeather for \1h\1c' + res.name + '\1n\1r: '
+ '\1h\1c' + res.weather[0].main + ' \1n\1r(\1c' + res.weather[0].description + '\1r), '
+ '\1h\1c' + res.clouds.all + '% cloudy\1n\1r, '
+ 'Current temp: \1h\1c' + temperature_str(res.main.temp) + '\1n\1r, '
+ 'Min temp: \1h\1c' + temperature_str(res.main.temp_min) + '\1n\1r, '
+ 'Max temp: \1h\1c' + temperature_str(res.main.temp_max) + '\1n\1r, '
+ 'Wind: \1h\1c' + res.wind.speed + ' KM/h ' + owm.wind_direction(res.wind.deg) + '\1n\1r, '
+ 'Humidity: \1h\1c' + res.main.humidity + '%\1n\1r, '
+ 'Pressure: \1h\1c' + res.main.pressure + ' hPa\1n\1r, '
+ 'Sunrise: \1h\1c' + system.timestr(res.sys.sunrise) + '\1n\1r, '
+ 'Sunset: \1h\1c' + system.timestr(res.sys.sunset)
+ ' \1n\1m(\1h\1mProvided by OpenWeatherMap.org\1n\1m)';
srv.o(target, ctrl_a_to_mirc(str));
} catch (err) {
log(LOG_DEBUG, 'Failed to display weather conditions: ' + err);
srv.o(target, 'Failed to fetch weather conditions: ' + err);
......
......@@ -2,7 +2,7 @@ if (!js.global.get_geoip) js.global.load(js.global, "geoip.js");
if (!js.global.get_nicklocation) js.global.load(js.global, "nicklocate.js");
if (!js.global.OpenWeatherMap) js.global.load(js.global, 'openweathermap.js');
const owm = new OpenWeatherMap();
var owm = new OpenWeatherMap();
function locate_user(nick, srv) {
const ret = { units: 'metric' };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment