3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 22:39:26 +01:00
dbot/modules/weather/weather.js
Kagammor c7733ee382 Don't show comma if no city is given
The OpenWeatherMap API may return weather for areas unlinked to a city, on which body.name will be undefined. This would previously show up as (for example) "[ , United Kingdom]". This simple ternary will only return a comma if it is also returning a city name.
2015-02-17 01:23:51 +01:00

36 lines
1.2 KiB
JavaScript

/**
* Module Name: weather
* Description: weather and ting
*/
var request = require('request'),
_ = require('underscore')._;
var weather = function(dbot) {
this.ApiRoot = 'http://api.openweathermap.org/data/2.5/';
this.commands = {
'~weather': function(event) {
var city = event.input[1];
request.get({
'url': this.ApiRoot + 'weather',
'qs': {
'q': city,
'units': 'metric'
},
'json': true
}, function(err, response, body) {
if(!err && body && _.has(body, 'cod') && body.cod === 200) {
event.reply('['+(body.name ? body.name +', ':'')+body.sys.country+']' + ' Condition: ' + body.weather[0].description + ' | Temp: ' + Math.round(body.main.temp) + 'C/'+Math.round(body.main.temp* 9 / 5 + 32)+'F | Humidity: ' + body.main.humidity + '% | Wind Speed: ' + body.wind.speed + 'KM/H');
} else {
event.reply('There is no weather in ' + city);
}
});
}
};
this.commands['~weather'].regex = [/^weather (.*)/, 2];
};
exports.fetch = function(dbot) {
return new weather(dbot);
};