-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtime.js
executable file
·207 lines (155 loc) · 5.91 KB
/
time.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
var request = require('request');
var config = require('../config');
var countries = require('country-list')();
const apikey = config.google.key;
const geocodeURL = "https://maps.googleapis.com/maps/api/geocode/json"; //GET address=target
const timezoneURL = "https://maps.googleapis.com/maps/api/timezone/json"; //GET location=lat,lng, timestamp=current
const
_description = "Get local time of basically anywhere. - By @vmednis aka kweakzsz",
_help = {
"/time": "Displays the local time of anywhere. By @vmednis aka kweakzsz"
}
;
function atLeastTwoDigits(num) {
return num / 10 < 1 ? "0" + num : num;
}
var TimePlugin = function(data){
var that = this;
that.bot = data.bot;
that.data = data;
that.help = _help;
that.getGeolocation = function(addr, done) {
request.get({
url: geocodeURL,
qs: {
address: addr,
key: apikey
},
json: true
}, function(err, resp, body) {
if(err) return done(err);
if(body.status != "OK") {
if(body.status == "REQUEST_DENIED") return done(body);
else return done(null, null);
}
var lat, lng, formatted_address;
lat = body.results[0].geometry.location.lat;
lng = body.results[0].geometry.location.lng;
formatted_address = body.results[0].formatted_address;
return done(null, { lat: lat, lng: lng, formatted_address: formatted_address });
});
};
that.getGeoTime = function(time, geoloc, done) {
if(!geoloc) return;
//The geolocation of address is already known
request.get({
url: timezoneURL,
qs: {
location: geoloc.lat + "," + geoloc.lng,
timestamp: time,
key: apikey
},
json: true
}, function(err, resp, body) {
if(err) return done(err);
if(body.status != "OK") {
if(body.status == "REQUEST_DENIED") return done(body);
else return done(null, null);
}
return done(null, body);
});
};
that.formatGeoTime = function(time, geoTime) {
let
rawOffset = geoTime.rawOffset,
dstOffset = geoTime.dstOffset,
localTime
;
//Calculate the time from offsets
localTime = new Date((time + dstOffset + rawOffset) * 1000);
return atLeastTwoDigits(localTime.getUTCHours()) + ":" + atLeastTwoDigits(localTime.getUTCMinutes());
}
that.commands = {
"/time": function(input, user, message, isHelp) {
//Validate user input
if(!input || input == "" || isHelp) {
var msg = TimePlugin.helpMessage();
that.bot.emit("do:commandResponsePM", msg.str, user, {
htmlMessage: msg.html
});
return;
}
if(input && input.charAt(0) == "@") {
if(that.data.room && that.data.room.users) {
var requestedUser = that.data.room.users.find(function(u){
var uname = u.username || u.id || u.uri.split(":")[2];
return (uname && "@"+uname.trim().toLowerCase() == input.toLowerCase().trim());
});
if(requestedUser && requestedUser.country) {
input = countries.getName(requestedUser.country||"GB");
}
}
}
let utctime = Math.floor(Date.now() / 1000), addr = input.trim(); // In seconds for google api
that.getGeolocation(input, function(err, geo){
if(err) {
that.bot.emit("error", err);
}
if(!geo) {
return that.bot.emit("do:commandResponseNotice", "Couldn't find a place called " + addr + ".");
}
that.getGeoTime(utctime, geo, function(err, result){
if(err || !result) {
if(err) that.bot.emit("error", err);
return that.bot.emit("do:commandResponsePM", "Sorry, couldn't find timezone info for " + addr + ".");
}
let response = "Current time in " + geo.formatted_address + " is " + that.formatGeoTime(utctime, result) + ".";
that.bot.emit("do:commandResponseNotice", response);
});
});
}
};
return that;
};
TimePlugin.help = _help;
TimePlugin.description = _description;
module.exports = TimePlugin;
// This can be tested locally, like so:
// TimePlugin.helpMessage = function(name, help, description){
// return {
// html: "Nice Help",
// str: "Nice Help"
// }
// };
// var TP = new TimePlugin({
// bot:{
// on: console.log,
// emit: console.log
// },
// currentTrack: {
// artists: [
// {
// name: "Cage The Elephant"
// }
// ],
// name: "James Brown"
// },
// room: {
// users: [
// {
// username: "testbro",
// uri: "testbro",
// country: "SG"
// }
// ]
// }
// });
// TP.commands["/time"]("Las Vegas, NV", {uri: "123"});
// TP.commands["/time"]("fresno", {uri: "123"});
// TP.commands["/time"]("asdfasdf", {uri: "123"});
// TP.commands["/time"]("123", {uri: "123"});
// TP.commands["/time"]("JJJFDKI8888****", {uri: "123"});
// TP.commands["/time"]("help", {uri: "123"}, "/time help", true);
// TP.commands["/time"]("Brīvības piemineklis", {uri: "123"});
// TP.commands["/time"]("Brīvības piemineklis", {uri: "123"});
// TP.commands["/time"]("@testbro", {uri: "123"}, "@testbro");