-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (70 loc) · 1.92 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
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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import discord, {
sendMessage,
discordNotification,
discordChatline,
discordBesttime,
discordBestmultitime,
discordBattlestart,
discordBattlequeue,
discordBattleresults,
discordBattleEnd,
} from './src/discord.js';
import config from './src/config';
// start express and middleware
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
// start discord bot
discord();
const checkAuth = (req, res, next) => {
if (req.header('Authorization') === config.auth) {
return next();
}
return res.sendStatus(401);
};
// endpoints
app.post('/sendmessage', checkAuth, (req, res) => {
const channelId = config.discord?.channels?.[req.body.channel];
if (channelId) {
sendMessage(channelId, req.body.message);
res.sendStatus(201);
return;
}
res.sendStatus(400);
});
app.post('/sendnotification', checkAuth, async (req, res) => {
if (req.body.discordId && req.body.type && req.body.meta) {
await discordNotification(req.body.discordId, req.body.type, req.body.meta);
res.sendStatus(201);
return;
}
res.sendStatus(400);
});
app.post('/gameevent', checkAuth, (req, res) => {
const types = {
API_chatline: discordChatline,
API_besttime: discordBesttime,
API_bestmultitime: discordBestmultitime,
API_battlestart: discordBattlestart,
API_battleresults: discordBattleresults,
API_battlequeue: discordBattlequeue,
API_battleend: discordBattleEnd,
};
if (types[req.body.event]) {
types[req.body.event](req.body);
res.sendStatus(201);
return;
}
res.sendStatus(400);
});
app.get('*', (req, res) => {
res.send('Oh, I exist? I thought I felt different today.');
});
app.listen(config.port, () =>
// eslint-disable-next-line no-console
console.log(`Example app is listening on port ${config.port}.`),
);