2022-12-12 01:25:48 +00:00
|
|
|
const Command = require('../../base/Command.js');
|
2022-12-11 23:22:28 +00:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const windrose = require('windrose');
|
|
|
|
const ISO2 = require('../../assets/ISO2.json');
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = class Weather extends Command {
|
|
|
|
constructor (name, category) {
|
2022-12-12 01:25:48 +00:00
|
|
|
super (name, category);
|
2022-12-11 23:22:28 +00:00
|
|
|
this.name = name,
|
|
|
|
this.description = 'View the weather in a place ',
|
|
|
|
this.options = [
|
|
|
|
{
|
|
|
|
type: 3,
|
2022-12-12 01:25:48 +00:00
|
|
|
name: 'city',
|
|
|
|
description: 'The city to check the weather at',
|
2022-12-11 23:22:28 +00:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 3,
|
2022-12-12 01:25:48 +00:00
|
|
|
name: 'country',
|
|
|
|
description: 'The country the city is in',
|
2022-12-11 23:22:28 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
this.category = category,
|
2022-12-12 01:25:48 +00:00
|
|
|
this.cooldown = 10000;
|
|
|
|
}
|
2022-12-11 23:22:28 +00:00
|
|
|
|
|
|
|
async run (client, interaction, data) { //eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
const city = await interaction.options.get('city').value;
|
|
|
|
let country = await interaction.options.get('country');
|
2022-12-12 01:25:48 +00:00
|
|
|
let countryCode = '';
|
2022-12-11 23:22:28 +00:00
|
|
|
|
|
|
|
if (country) {
|
|
|
|
country = country.value;
|
2022-12-12 01:25:48 +00:00
|
|
|
countryCode += ',';
|
2022-12-11 23:22:28 +00:00
|
|
|
if (ISO2.country[country.toProperCase().trim()]) {
|
|
|
|
countryCode += ISO2.country[country.toProperCase().trim()];
|
|
|
|
} else {
|
|
|
|
countryCode += country.trim();
|
|
|
|
}
|
2022-12-12 01:25:48 +00:00
|
|
|
}
|
2022-12-11 23:22:28 +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 }})
|
2022-12-12 01:25:48 +00:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(json => {
|
|
|
|
if (json.cod >= 200 && json.cod <= 299) {
|
|
|
|
const tempCelsius = Math.round(json.main.temp - 273.15);
|
|
|
|
let embedColor;
|
|
|
|
if (tempCelsius < 0) {
|
|
|
|
embedColor = '#addeff';
|
|
|
|
} else if (tempCelsius < 20) {
|
|
|
|
embedColor = '#4fb8ff';
|
|
|
|
} else if (tempCelsius < 26) {
|
|
|
|
embedColor = '#ffea4f';
|
|
|
|
} else if (tempCelsius < 31) {
|
|
|
|
embedColor = '#ffa14f';
|
|
|
|
} else {
|
|
|
|
embedColor = '#ff614f';
|
|
|
|
}
|
|
|
|
|
|
|
|
const embed = new client.EmbedBuilder()
|
|
|
|
.setTitle('Current conditions in ' + city.toProperCase() + ', ' + ISO2.code[json.sys.country])
|
|
|
|
.setThumbnail(`https://openweathermap.org/img/wn/${json.weather[0].icon}@4x.png`)
|
|
|
|
.setColor(embedColor)
|
|
|
|
.addFields([
|
|
|
|
{ name: 'Condition:', value: json.weather[0].main, inline: true },
|
|
|
|
{ name: 'Temperature:', value: `${tempCelsius}°C ・ ${Math.round(json.main.temp * 9/5 - 459.67)}°F`, inline: true },
|
|
|
|
{ name: 'Min/Max:', value:`
|
|
|
|
${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
|
|
|
|
`, inline: true},
|
|
|
|
{ name: 'Humidity:', value: `${json.main.humidity}%`, inline: true },
|
|
|
|
{ name: 'Wind Speed:', value: `${Math.round(json.wind.speed * 10) / 10}km/h ・ ${Math.round(json.wind.speed * 10 / 1.609344)}mi/h`, inline: true },
|
|
|
|
{ name: 'Wind Direction:', value: windrose.getPoint(json.wind.deg).name, inline: true}
|
|
|
|
])
|
|
|
|
.setFooter({ text: 'Powered by openweathermap.org'});
|
|
|
|
|
|
|
|
return interaction.reply({embeds: [embed]});
|
2022-12-11 23:22:28 +00:00
|
|
|
} else {
|
2022-12-12 01:25:48 +00:00
|
|
|
if (json.message && json.message === 'city not found') {
|
|
|
|
return interaction.reply({
|
|
|
|
content: `${client.config.emojis.userError} ${city.toProperCase()} is not listed in my sources.`,
|
|
|
|
ephemeral: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
client.logger.error('WEATHER_COMMAND_ERROR', `API Error: ${json}`);
|
2022-12-12 01:08:37 +00:00
|
|
|
return interaction.reply({
|
2022-12-12 01:25:48 +00:00
|
|
|
content: `${client.config.emojis.botError} API error occurred: \`code ${json.cod}\``,
|
2022-12-12 01:08:37 +00:00
|
|
|
ephemeral: true
|
|
|
|
});
|
2022-12-12 01:25:48 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-11 23:22:28 +00:00
|
|
|
|
2022-12-12 01:25:48 +00:00
|
|
|
}
|
2022-12-11 23:22:28 +00:00
|
|
|
};
|