-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestuff.js
86 lines (75 loc) · 2.87 KB
/
timestuff.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
var cityTimezones = require('./citytime.js');
var timezone = require('node-google-timezone');
function pad(n) {
if (n < 10)
return "0" + n.toString();
else
return n.toString();
}
function cityTime(message) {
var city = message.content.substring(7);
const citydata = cityTimezones.lookupViaCity(city);
try {
var lati = citydata[(city.toLowerCase() === 'london' || city.toLowerCase() === 'san antonio') ? 1 : 0].lat;
var lngi = citydata[(city.toLowerCase() === 'london' || city.toLowerCase() === 'san antonio') ? 1 : 0].lng;
var timestamp = Date.now() / 1000;
timezone.data(lati, lngi, timestamp, function (err, tz) {
if (!err) {
var d = new Date(tz.local_timestamp * 1000);
message.channel.send(d.toDateString() + ' - ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()));
} else {
message.channel.send(err);
}
});
} catch (error) {
message.channel.send('Error: ' + error.message);
}
}
function localcityTime(message, city) {
const citydata = cityTimezones.lookupViaCity(city);
try {
var lati = citydata[city === 'London' ? 1 : 0].lat;
var lngi = citydata[city === 'London' ? 1 : 0].lng;
var timestamp = Date.now() / 1000;
timezone.data(lati, lngi, timestamp, function (err, tz) {
if (!err) {
var d = new Date(tz.local_timestamp * 1000);
message.channel.send(d.toDateString() + ' - ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()));
} else {
message.channel.send(err);
}
});
} catch (error) {
message.channel.send('Error: ' + error.message);
}
}
module.exports = (message, content, herokupg) => {
if (content.toLowerCase().substring(0, 8) === 'settime ' && content.length > 8) {
var city = content.substring(8).replace(/[^A-Za-z ]+/gm, '');
console.log('INSERT INTO localtimes(user_id, city_name) VALUES (\'' + message.author.id + '\',\'' + city + '\')ON CONFLICT (user_id) DO UPDATE SET city_name = EXCLUDED.city_name;');
herokupg.query('INSERT INTO localtimes(user_id, city_name) VALUES (\'' + message.author.id + '\',\'' + city + '\')ON CONFLICT (user_id) DO UPDATE SET city_name = EXCLUDED.city_name;', (err, res) => {
if (!err) {
console.log(res);
message.channel.send('Beabo bee! (Local time set for <@' + message.author.id + '>)');
} else
console.log(err);
});
}
if (content.substring(0, 10) === 'gettime <@' && content.length > 9) {
var id = content.substring(10).replace(/[^0-9]/gm, '');
herokupg.query('SELECT city_name FROM localtimes WHERE user_id = \'' + id + '\';', (err, res) => {
if (!err) {
console.log(res);
if (res.rows.length > 0){
localcityTime(message, res.rows[0].city_name);
} else {
message.channel.send('Bee bee! (error. could not find local time for <@' + id + '>)');
}
} else
console.log(err);
});
}
if (content.substring(0, 5) === 'time ') {
cityTime(message);
}
}