Files
gestion/commands/utils/weather.js
T
2024-03-01 21:06:34 +01:00

39 lines
1.5 KiB
JavaScript

const Discord = require('discord.js');
const axios = require('axios');
module.exports = {
name: 'weather',
description: 'Affiche les informations météorologiques d\'une ville',
category: 'utils',
emote: '☀️',
utilisation: 'weather [ville]',
async execute(message, args) {
try {
const APIKEY = '1e59407044fd6d842180610a8c423aa4';
const city = args[0];
if (!city) {
return message.channel.send('Veuillez fournir une ville.');
}
const response = await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&appid=${APIKEY}&units=metric`);
const weatherData = response.data;
const cityName = weatherData.name;
const temperature = weatherData.main.temp;
const weatherDescription = weatherData.weather[0].description;
const Embed = new Discord.MessageEmbed()
.setTitle(`Meteo à ${cityName}`)
.setDescription(`🌡・Temperature: ${temperature}°C\n⛅・Temps: ${weatherDescription}`)
.setTimestamp()
.setColor('RANDOM') // Discord.js uses 'RANDOM' for random colors
.setFooter({text: message.client.user.username, iconURL: message.client.user.displayAvatarURL({dynamic: true})});
await message.channel.send({ embeds: [Embed] });
} catch(e) {
console.error(e);
await message.channel.send(`Je n'ai pas trouvé la ville ${city}!`);
}
},
};