-
Notifications
You must be signed in to change notification settings - Fork 27
/
commands.js
280 lines (256 loc) · 7.13 KB
/
commands.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict';
/**
* Requires
*/
const rp = require('request-promise');
const moment = require('moment');
const config = require('./config');
const helper = require('./helper');
/**
* Commands
*/
module.exports = {
/**
* Fake a error promise
*
* @param {string} error Error Message
*
* @return {object} Rejected Request Promise
*/
error(error) {
return Promise.reject(new Error(error));
},
/**
* Get Bus Arrival Timing
*
* @param {object} commandArguments Command Arguments
*
* @return {object} Request promise
*/
bus(commandArguments) {
const busStopNo = commandArguments[0];
const busNo = commandArguments[1] || '';
let busQuery = busStopNo;
if (busNo !== '') {
busQuery += `/${busNo}`;
}
return rp({
uri: `${config.lesterchanApiUrl}/lta/bus-arrival/${busQuery}`,
json: true,
}).then((body) => {
if (body.Services && body.Services.length > 0) {
const busLoad = {
SEA: 'Seats Available',
SDA: 'Standing Available',
LSD: 'Limited Standing',
};
const busType = {
SD: 'Single Deck',
DD: 'Double Deck',
BD: 'Bendy',
};
// Fields
const fields = [];
body.Services.forEach((bus) => {
fields.push({
title: 'Bus',
value: bus.ServiceNo,
});
// Bus Arrival Timings
const nextBus = bus.NextBus || '';
const subBus = bus.NextBus2 || '';
const followBus = bus.NextBus3 || '';
if (nextBus !== '') {
fields.push({
title: 'Next Bus',
value: `${moment(nextBus.EstimatedArrival).fromNow()} (${busLoad[nextBus.Load]}, ${busType[nextBus.Type]})`,
});
} else {
fields.push({
title: 'Next Bus',
value: 'Not Operating Now',
});
}
if (subBus !== '') {
fields.push({
title: 'Subsequent Bus',
value: `${moment(subBus.EstimatedArrival).fromNow()} (${busLoad[subBus.Load]}, ${busType[subBus.Type]})`,
});
} else {
fields.push({
title: 'Subsequent Bus',
value: 'Not Operating Now',
});
}
if (followBus !== '') {
fields.push({
title: 'Following Bus',
value: `${moment(followBus.EstimatedArrival).fromNow()} (${busLoad[followBus.Load]}, ${busType[followBus.Type]})`,
});
} else {
fields.push({
title: 'Following Bus',
value: 'Not Operating Now',
});
}
});
return helper.formatMessage(`Bus Stop ${body.BusStopCode}`, '', fields);
}
return 'Bus stop or number is invalid';
});
},
/**
* Haze
*
* @return {object} Request promise
*/
haze() {
return rp({
uri: `${config.lesterchanApiUrl}/nea/psipm25`,
json: true,
}).then((body) => {
// Variables
const northPsi = parseInt(body.items[0].readings.pm25_one_hourly.north, 10);
const centralPsi = parseInt(body.items[0].readings.pm25_one_hourly.central, 10);
const eastPsi = parseInt(body.items[0].readings.pm25_one_hourly.east, 10);
const westPsi = parseInt(body.items[0].readings.pm25_one_hourly.west, 10);
const southPsi = parseInt(body.items[0].readings.pm25_one_hourly.south, 10);
const averagePsi = Math.ceil((northPsi + centralPsi + eastPsi + westPsi + southPsi) / 5);
const { timestamp } = body.items[0];
const niceDate = moment(timestamp).add(8, 'hours');
// Fields
const fields = [
{
title: 'Average',
value: helper.getMessage(averagePsi),
},
{
title: 'Central',
value: helper.getMessage(centralPsi),
},
{
title: 'North',
value: helper.getMessage(northPsi),
},
{
title: 'South',
value: helper.getMessage(southPsi),
},
{
title: 'East',
value: helper.getMessage(eastPsi),
},
{
title: 'West',
value: helper.getMessage(westPsi),
},
];
return helper.formatMessage('Singapore Haze Conditions', `PM2.5 Hourly Update. Last updated at ${niceDate.format(config.defaultDateTimeFormat)}.`, fields);
});
},
/**
* Weather (2 hour Forecast)
*
* @return {object} Request promise
*/
weather() {
return rp({
uri: `${config.lesterchanApiUrl}/nea/nowcast`,
json: true,
}).then((body) => {
const fields = [];
if (body.items[0].forecasts && body.items[0].forecasts.length > 0) {
body.items[0].forecasts.forEach((nowcast) => {
fields.push({
title: nowcast.area,
value: helper.getMessage(nowcast.forecast),
});
});
}
return helper.formatMessage('Singapore Weather Conditions', `2 hour Forecast. ${moment(body.items[0].update_timestamp).add(8, 'hours').format(config.defaultDateTimeFormat)}.`, fields);
});
},
/**
* IP Info
*
* @param {object} commandArguments Command Arguments
*
* @return {object} Request promise
*/
ipinfo(commandArguments) {
// Variables
const ip = commandArguments[0] || '127.0.0.1';
// Validate IP Address
if (!helper.validateIp(ip)) {
return this.error('Invalid IP');
}
return rp({
uri: `http://ipinfo.io/${ip}/json`,
json: true,
}).then((body) => {
// Fields
const fields = [
{
title: 'IP',
value: helper.getMessage(body.ip),
},
{
title: 'Hostname',
value: helper.getMessage(body.hostname),
},
{
title: 'Country',
value: helper.getMessage(body.country),
},
{
title: 'City',
value: helper.getMessage(body.city),
},
{
title: 'Region',
value: helper.getMessage(body.region),
},
{
title: 'Organization',
value: helper.getMessage(body.org),
},
];
return helper.formatMessage('IP Information', body.ip, fields);
});
},
/**
* Social Site Sharing Count
*
* @param {object} commandArguments Command Arguments
*
* @return {object} Request promise
*/
socialstats(commandArguments) {
const link = commandArguments[0] || 'https://lesterchan.net';
return rp({
uri: `${config.lesterchanApiUrl}/link?page=${link}`,
json: true,
}).then((body) => {
// Fields
const fields = [
{
title: 'Total',
value: helper.formatNumber(body.total_count),
},
{
title: 'Facebook',
value: helper.formatNumber(body.count.facebook),
},
{
title: 'Twitter',
value: helper.formatNumber(body.count.twitter),
},
{
title: 'Pinterest',
value: helper.formatNumber(body.count.pinterest),
},
];
return helper.formatMessage('Link Social Stats', body.url, fields);
});
},
};