This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckCal.js
82 lines (65 loc) · 2.04 KB
/
checkCal.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
var request = require('request');
const { MongoClient, ServerApiVersion } = require("mongodb");
const mongouri = process.env.mongooseURI;
const clientMain = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
const connection = clientMain.connect();
function getDateInMin() {
const d = new Date();
d.setMilliseconds(0);
d.setSeconds(0);
return (d.getTime()).toString();
}
function checkCal() {
try {
connection.then(client => {
const dbo = client.db('main').collection('reminders');
//Get all events happening this minute
const time = getDateInMin();
dbo.findOne({ time: time }).then((docs) => {
//Get All Events happening this MINUTE and put them into the following format
if (!docs) { return; }
var m = {};
for (i in docs) {
var doc = docs[i];
if (isNaN(Number(i))) { continue; }
//Get the time out of ms
doc.time = (Number(time) + ((Number(doc.offset)/* + 1*/) * 60000)) / 1000;
m[`${i}`] = doc;
}
var clientServerOptions = {
uri: 'https://selmer-bot-listener.ion606.repl.co/reminders/',
body: "",
method: 'POST',
headers: {
'botcode': process.env.accesscode,
'reminders': JSON.stringify(m)
}
}
request(clientServerOptions, function(error, response) {
// console.log(error, response.body);
return;
});
});
});
// client.close();
} catch (err) {
console.error(err);
}
}
//Make sure the app doesn't go to sleep
const express = require("express");
const app = express();
app.get('/', async (req, res) => {
return res.sendStatus(200);
})
const listener = app.listen(process.env.PORT, () => {
//Start at exactly the minute mark
while (true) {
let d = new Date();
if (d.getSeconds() == 0) {
break;
}
}
console.log("Your app is listening on port " + listener.address().port);
setInterval(checkCal, 60000);
});