-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappHandler.js
124 lines (107 loc) · 3.42 KB
/
appHandler.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require('express');
const PORT = process.env.PORT || 80;
const { URLSearchParams } = require('url');
const fetch = require('node-fetch');
const app = express();
const server = require('http').createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: true
}
})
let interval;
io.on('connection', async (client) => {
client.on('findQueue', async () => {
setInterval(async () => {
const queue = await queueController.queueExists()
client.emit('queueFound', queue);
}, 1000);
});
});
const connections = require('./connections.json')
const playerModel = require('./models/Player')
const queueModel = require('./models/Queue')
const playerController = require('./controllers/PlayerController')
const queueController = require('./controllers/QueueController')
const discordController = require('./controllers/DiscordController')
const utilsBot = require('./utils/bot')
const utilsRiot = require('./utils/riot')
const cors = require('cors');
const bodyParser = require('body-parser')
app.use(bodyParser());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/getRanking', async (req, res) => {
const result = await playerController.playersRankedPortal()
res.send(result)
})
app.get('/getPlayerById/:id', async (req, res) => {
const result = await playerController.getObjPlayer(req.params.id)
res.send(result)
})
app.get('/getAuth/:id', async (req, res) => {
const result = await playerController.getPlayerByDiscordToken(req.params.id)
res.send(result)
})
app.get('/auth', (req, res) => {
const code = req.query.code;
if (code) {
const data = {
client_id: connections.discordClientId,
client_secret: connections.discordSecretId,
grant_type: "authorization_code",
code: code,
scope: 'identify+guilds'
}
fetch('https://discord.com/api/oauth2/token', {
method: 'POST',
body: new URLSearchParams(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.json())
.then(data => res.redirect(`/guilds?token=${data.access_token}`));
} else {
res.send([
'https://discordapp.com/oauth2/authorize',
`?client_id=${connections.discordClientId}`,
'&scope=identify+guilds',
'&response_type=code'
].join(''));
}
});
app.post('/join', (req, res) => {
discordController.Queue.add({id: req.body.id})
res.send()
})
app.post('/queue', (req, res) => {
queueController.createQueue(req.body.id, req.body.size, false)
res.send()
})
app.post('/leave', (req, res) => {
queueController.leaveQueue(req.body.id)
res.send()
})
app.get('/guilds', (req, res) => {
fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `Bearer ${req.query.token}`,
},
})
.then(res => res.json())
.then(data => playerController.getPlayerById(data.id).then(result => {
result.discordAcessToken = req.query.token
playerModel.findOneAndUpdate({ id: data.id }, result, { new: true }).then(res.redirect(
`https://araminhouse.herokuapp.com/login/${req.query.token}`
// `https://araminhouse.herokuapp.com/login/${req.query.token}`
))
}
));
});
server.listen(PORT, () => console.log(`Listening on Port ${PORT}`));