woomy-v2/bot/commands/Utility/weather.js

86 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-10-30 03:39:43 +00:00
const fetch = require('node-fetch');
const windrose = require('windrose');
2020-11-10 03:31:49 +00:00
const ISO2 = require('../../assets/ISO2.json');
2020-10-30 03:39:43 +00:00
module.exports = class {
constructor (name, category) {
this.name = name,
this.category = category,
this.enabled = true,
this.devOnly = false,
this.aliases = [],
this.userPerms = [],
this.botPerms = [],
this.cooldown = 2000,
this.help = {
description: 'Gives you the weather for the specified city. You can also specify a country code with a comma.',
arguments: '<city>, [code]',
details: '`<city>` - name of a city\n`[code]` - country code (USA = US, Australia = AU, etc.)',
examples: 'w!weather Minneapolis\nw!weather Melbourne, AU'
2020-10-30 03:39:43 +00:00
};
}
run (client, message, args, data) { //eslint-disable-line no-unused-vars
if (!args[0]) return;
let city = args.join(' ').toProperCase();
let countryCode = ',';
2020-10-30 03:39:43 +00:00
if (args.join(' ').indexOf(',') > -1) {
const params = city.split(',');
city = params[0].trim().toProperCase();
if (ISO2.country[params[1].toProperCase().trim()]) {
countryCode += ISO2.country[params[1].toProperCase().trim()];
} else {
countryCode += params[1].trim();
}
console.log(countryCode);
2020-10-30 03:39:43 +00:00
}
message.channel.sendTyping();
2021-02-26 02:17:20 +00:00
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city + countryCode}&appid=${client.config.keys.weather}`, { headers: { 'User-Agent': client.config.userAgent }})
2020-10-30 03:39:43 +00:00
.then(res => res.json())
.then(json => {
if (json.cod >= 200 && json.cod <= 299) {
const tempCelcius = Math.round(json.main.temp - 273.15);
let embedColour;
if (tempCelcius < 0) {
embedColour = '#addeff';
} else if (tempCelcius < 20) {
embedColour = '#4fb8ff';
} else if (tempCelcius < 26) {
embedColour = '#ffea4f';
} else if (tempCelcius < 31) {
embedColour = '#ffa14f';
} else {
embedColour = '#ff614f';
}
2020-11-10 03:31:49 +00:00
const embed = new client.RichEmbed()
.setTitle(`Weather for ${city + ', ' + ISO2.code[json.sys.country]}`)
2020-10-30 03:39:43 +00:00
.setThumbnail(`https://openweathermap.org/img/wn/${json.weather[0].icon}@4x.png`)
.setColour(embedColour)
2020-11-06 03:42:52 +00:00
.addField('Condition:', json.weather[0].main, true)
.addField('Temperature:', `${tempCelcius}°C | ${Math.round(json.main.temp * 9/5 - 459.67)}°F`, true)
.addField('Min/Max:', `
2020-10-30 03:39:43 +00:00
${Math.round(json.main.temp_min - 273.15)}°C - ${Math.round(json.main.temp_max - 273.15)}°C
${Math.round(json.main.temp_min * 9/5 - 459.67)}°F - ${Math.round(json.main.temp_max * 9/5 - 459.67)}°F
`, true)
2020-11-06 03:42:52 +00:00
.addField('Humidity:', `${json.main.humidity}%`, true)
.addField('Wind Speed:', `${Math.round(json.wind.speed * 10) / 10}km/h | ${Math.round(json.wind.speed * 10 / 1.609344)}mi/h`, true)
.addField('Wind Direction:', windrose.getPoint(json.wind.deg).name, true)
2020-10-30 03:39:43 +00:00
.setFooter('Powered by openweathermap.org');
return message.channel.createMessage({ embed:embed });
} else {
if (json.message && json.message === 'city not found') {
2020-11-10 03:31:49 +00:00
return message.channel.createMessage(`${client.emojis.userError} You provided an invalid city name. Maybe check your spelling?`);
2020-10-30 03:39:43 +00:00
}
2020-11-10 03:31:49 +00:00
return message.channel.createMessage(`${client.emojis.botError} API error occured: \`code ${json.cod}: ${json.message}\``);
2020-10-30 03:39:43 +00:00
}
})
.catch(err => {
2020-11-10 03:31:49 +00:00
return message.channel.createMessage(`${client.emojis.botError} An error has occured: \`${err.stack}\``);
2020-10-30 03:39:43 +00:00
});
}
};