This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.ts
95 lines (84 loc) · 2.86 KB
/
webserver.ts
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
import expressws from 'express-ws';
import express from 'express';
import MongoManager from './mongo';
function getNearestMcdonalds(lat: string, lon: string) {
return new Promise((resolve, reject) => {
const url = (lat: string, lon: string) => `https://www.mcdonalds.com/googleappsv2/geolocation?latitude=${lat}&longitude=${lon}&radius=${10}&maxResults=${1}&country=de&language=en-us`
fetch(url(lat, lon), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'accept': '*/*'
}
}).then(res => {
res.text().then(text => {
const json = JSON.parse(text);
resolve(json.features[ 0 ].properties);
})
})
});
}
(async () => {
const mongo = new MongoManager('mongodb://mongo:27017');
await mongo.connect()
const app = expressws(express()).app;
app.use(express.json())
app.use(express.static('./public'));
app.get('/getcodes/', async (req, res) => {
res.send(await mongo.getUnusedCodes());
})
app.patch('/mark/:code', async (req, res) => {
await mongo.markUsed(req.params.code);
const nearest: any = await getNearestMcdonalds(req.body.lat, req.body.long);
const params = {
embeds: [ {
title: "Code used",
description: req.params.code,
fields: [
{
name: "Location",
value: `${req.body.lat}, ${req.body.long}`
},
{
name: "Nearest McDonald's",
value: `${nearest.addressLine1}`
},
{
name: "User Agent",
value: req.body.userAgent
}
]
} ]
}
fetch("https://discord.com/api/webhooks/", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
})
res.send();
})
app.get('/getrandomcode/', async (req, res) => {
const codes = await mongo.getUnusedCodes();
const code = codes[ Math.floor(Math.random() * codes.length) ] as any;
code[ 'count' ] = codes.length;
res.send(code);
})
app.post('/newcode/:code', async (req, res) => {
const params = {
content: "New code: " + req.params.code
}
fetch("https://discord.com/api/webhooks/", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
})
res.send();
})
app.listen(1337, () => {
console.log('listening on http://localhost:1337/');
});
})()