-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanelServerToMongo.js
74 lines (62 loc) · 2.46 KB
/
PanelServerToMongo.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
const clientModel = require("./../database/models/clientModel");
const axios = require("axios");
const panelClients = require("./../database/models/panelClients");
require("dotenv").config()
const fs = require('fs')
const util = require('util');
module.exports.getPanelUsers = async function () {
try {
let panelRequest = await axios.get(process.env.PTERO_URL + "application/servers", {
"headers": {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.PTERO_API_KEY
}
});
panelRequest.data.data.map(async server => {
getPanelForUser(server.attributes.user, server.attributes.username).then(async (datas) => {
panelClients.findOne({panelId: server.attributes.id}, async (err, data) => {
if (err) throw err;
if (!data) {
const newClient = new panelClients({
discordId: datas.discordId,
databaseId: server.attributes.id,
panelId: server.attributes.id,
});
await newClient.save();
}
else {
}
})
});
})
} catch (e) {
console.log(e.response.data.errors)
}
}
async function getPanelForUser(userid) {
try {
let request = await axios.get(process.env.PTERO_URL + "application/users/" + userid, {
"headers": {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.PTERO_API_KEY
}
})
clientModel.findOne({discordId: request.data.attributes.username}, async (err, data) => {
if (err) throw err;
if (!data) {
return null
} else {
let offres = data.offresCount;
offres++;
clientModel.findOneAndUpdate({discordId: request.data.attributes.username}, {offresCount: offres}, (err, data) => {
if (err) throw err;
})
}
})
return {discordId: request.data.attributes.username};
} catch (e) {
console.log(e.response.data.errors)
}
}