-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (38 loc) · 1.08 KB
/
index.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
const config = require('./config');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const { sendSubmissionToDiscord } = require('./functions');
app.use(bodyParser.json());
app.use(cors());
app.post('/:marathonId', (req, res) => {
const body = req.body;
const marathon = req.params.marathonId;
console.log(JSON.stringify(body));
handleWebhook(body, marathon);
res.send('OK');
});
async function handleWebhook(data, marathon) {
switch (data.event) {
case 'PING':
console.log('Got ping event');
break;
case 'DONATION':
console.log('Got donation event')
break;
case 'SUBMISSION_ADD':
try {
await sendSubmissionToDiscord(data.submission, marathon);
} catch (e) {
console.error(e);
}
console.log('submission added by ' + data.submission.user.username)
break;
case 'SUBMISSION_EDIT':
console.log('submission edited by ' + data.submission.user.username)
break;
}
}
app.listen(config.port);
console.log(`Listening on http://localhost:${config.port}/`);