-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.js
127 lines (106 loc) · 4.81 KB
/
weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { warn, debug } = asyncLogs;
const weatherApi = require('./modules/openWeatherMap'); // Weather data API
exports.run = function weather (_, msg, args) {
if (args.length < 1) throw "No arguments passed";
args = args.join(' ');
weatherApi.find({search: args, unitsType: 'metric'}, function(err, result) {
// Error Messages
if(err) {
warn(err);
if (!result) return msg.reply({
"embed": {
"title": ":warning: A critical error occurred!",
"description": "Check logs for more detailed information."
}
}); // Send error message if no info given
}
debug(JSON.stringify(result, null, 2));
if (result === undefined) return msg.reply({
"embeds": [
{
"title": ":warning: Error parsing data!",
"description": "Check logs for more detailed information."
}
]
});
if (typeof result.success !== 'boolean' || result.success === false) return msg.reply(`:warning: ${result.message || "An unspecified error occurred."}`);
if (!result.weather[0]) return msg.reply({
"embeds": [
{
"title": `:warning: ${result.message || "No data. An unknown error occurred."}`,
"description": "Check logs for more detailed information."
}
]
});
// SUCCESS! Sending the data now...
msg.reply({
"embeds": [
{
"title": `The Weather in **${result.name}, ${result.sys.country}**\n`,
"url": `https://openweathermap.org/city/${result.id}`,
"description": `**Sky**: ${result.weather[0].description}\n` +
((!isNaN(result.main.temp)) ? `**Temperature**: ${result.main.temp}°C\t[${result.main.temp_max}°C / ${result.main.temp_min}°C]\n` : ``) +
((!isNaN(result.main.feels_like)) ? `**Feels Like**: ${result.main.feels_like}°C\n` : ``) +
((!isNaN(result.main.humidity)) ? `**Humidity**: ${result.main.humidity}%\n` : ``) +
((!isNaN(result.main.pressure)) ? `**Pressure**: ${result.main.pressure}\u00A0hPa\n` : ``) +
((!isNaN(result.wind.speed)) ? `**Wind Speed**: ${result.wind.speed}\u00A0m/s [${result.wind.cardinal}]\n` : ``) +
((!isNaN(result.wind.kmSpeed)) ? `**Keeper's Wind**: ${result.wind.kmSpeed}\u00A0km/h from ${result.wind.leadDeg}°\n` : ``) +
((!isNaN(result.wind.gust)) ? `**Wind Gust**: ${result.wind.gust}\u00A0m/s\n` : ``) +
((!isNaN(result.visibility)) ? `**Visibility**: ${Math.floor(result.visibility / 1000)}\u00A0km` : ``),
"color": 0x41f097,
"timestamp": (new Date(result.dt * 1000)),
"thumbnail": {
"url": `https://openweathermap.org/img/wn/${result.weather[0].icon}@2x.png`
},
"image": { // Possible map in the future
"url": ""
},
"fields": [
{ // Sadly I need to use a cheat to force inline
"name": "\u200C",
"value": "\u200C",
"inline": true
},
{
"name": "City",
"value": `${result.name}`,
"inline": true
}, // Add States?
{
"name": "Country",
"value": `${result.sys.country}`,
"inline": true
},
{
"name": "\u200C",
"value": "\u200C",
"inline": true
},
{
"name": "Longitude",
"value": `${result.coord.lon}`,
"inline": true
},
{
"name": "Latitude",
"value": `${result.coord.lat}`,
"inline": true
}
]
}
]
})
});
};
exports.conf = {
enabled: true,
guildOnly: false,
permLevel: 1,
type: 5
};
exports.help = {
name: `weather`,
aliases: [],
description: `Get the weather informatin of a city.`,
usage: `weather <city name>, [<US state name [optional]>,] <country alpha-2 (ISO 3166)>`
};