diff --git a/exec/ircbots/weather/weather_commands.js b/exec/ircbots/weather/weather_commands.js index 735c7c7ede9f31808ec6b3c517232fd3ceef86eb..8cfb5e4646a63c0c4647ed5194194a814ed38dfa 100644 --- a/exec/ircbots/weather/weather_commands.js +++ b/exec/ircbots/weather/weather_commands.js @@ -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); diff --git a/exec/ircbots/weather/weather_functions.js b/exec/ircbots/weather/weather_functions.js index 471fe7d7e015a8025d949e91822abd2bd18b8349..ad48b07416beba37f87f68c9d5211ee8bcaf3020 100644 --- a/exec/ircbots/weather/weather_functions.js +++ b/exec/ircbots/weather/weather_functions.js @@ -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' };