MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / handler

Function handler

plugins/weather.js:9–43  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

7 description: 'Get the current weather for a specific city!',
8 usage: '.weather <city>',
9 async handler(sock, message, args, context) {
10 const chatId = context.chatId || message.key.remoteJid;
11 const city = args.join(' ').trim();
12 if (!city) {
13 return await sock.sendMessage(chatId, {
14 text: "*Please provide a place to search.*\nExample: .weather Karachi",
15 ...channelInfo
16 }, { quoted: message });
17 }
18 try {
19 const apiKey = '060a6bcfa19809c2cd4d97a212b19273';
20 const response = await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&units=metric&appid=${apiKey}`);
21 const weather = response.data;
22 const weatherText = `ʜᴇʀᴇ ɪs ʏᴏᴜʀ ᴘʟᴀᴄᴇ ᴡᴇᴀᴛʜᴇʀ\n\n` +
23 `「 🌅 」ᴘʟᴀᴄᴇ: ${weather.name}\n` +
24 `「 🗺️ 」ᴄᴏᴜɴᴛʀʏ: ${weather.sys.country}\n` +
25 `「 🌤️ 」ᴠɪᴇᴡ: ${weather.weather[0].description}\n` +
26 `「 🌡️ 」ᴛᴇᴍᴘᴇʀᴀᴛᴜʀᴇ: ${weather.main.temp}°C\n` +
27 `「 💠 」ᴍɪɴɪᴍᴜᴍ ᴛᴇᴍᴘᴇʀᴀᴛᴜʀᴇ: ${weather.main.temp_min}°C\n` +
28 `「 🔥 」ᴍᴀxɪᴍᴜᴍ ᴛᴇᴍᴘᴇʀᴀᴛᴜʀᴇ: ${weather.main.temp_max}°C\n` +
29 `「 💦 」ʜᴜᴍɪᴅɪᴛʏ: ${weather.main.humidity}%\n` +
30 `「 🌬️ 」ᴡɪɴᴅ sᴘᴇᴇᴅ: ${weather.wind.speed} km/h`;
31 await sock.sendMessage(chatId, {
32 text: weatherText,
33 ...channelInfo
34 }, { quoted: message });
35 }
36 catch (error) {
37 console.error('Weather plugin error:', error);
38 await sock.sendMessage(chatId, {
39 text: '❌ Sorry, I could not fetch the weather. Make sure the place name is correct.',
40 ...channelInfo
41 }, { quoted: message });
42 }
43 }
44};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected