-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathModSystem.js
188 lines (145 loc) · 5.11 KB
/
ModSystem.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* @file ModSystem.js
* @author suufi (Qxest)
* @description Generates the API using Express.js for the Moderation System used by Sphere
*/
// Token
const TOKEN = "tokomoki";
// Imports
const roblox = require("roblox-js");
const r = require("rethinkdbdash")({
db: "F3X"
});
// Express
const express = require("express");
const app = express();
const bans = express();
const bodyParser = require("body-parser");
// Middleware
app.use(bodyParser.json());
// Routes
/*
* GET /bans
* Body none
*
* Return back "Ban system!" when user visits /bans
*/
bans.get("/", (req, res) => {
return res.send("Ban system!");
});
/*
* POST /bans/ban
* Body userId, admin, reason, token
*
* Creates a document in the bans table containing the user being banned,
* the admin that banned them, the reason that they were banned, and the
* token used for verifying that the request is genuine.
*/
bans.post("/ban", async (req, res) => {
// Check if token was provided
if (!req.body.token !== TOKEN) return res.sendStatus(403);
// Check if all body parameters were provided
if (!req.body.userId && !req.body.admin && !req.body.reason) return res.sendStatus(400);
// Get the user's username
var username = await roblox.getUsernameFromId(req.body.userId);
// Query the ban table for the userId provided
r.table("bans").getAll(parseInt(req.body.userId), {index: "userId"}).then(bans => {
// Check if user is not already banned
if (bans.length === 0) {
// Insert to the bans table
r.table("bans").insert({
userId: req.body.userId,
username: username,
admin: req.body.admin,
reason: req.body.reason,
timestamp: r.now()
}).run().then(() => {
return res.send(200);
});
} else {
// Respond back that the person is already banned.
return res.send("User is already banned.");
}
}).catch(console.error);
});
/*
* DELETE /bans/ban
* Body userId, token
*
* Deletes the document previously created, if it exists, that refers
* to a banned user allowing the user to be unbanned in-game.
*/
bans.delete("/ban", (req, res) => {
// Check if token was provided
if (!req.body.token !== TOKEN) return res.sendStatus(403);
// Check if userId was provided in body
if (!req.body.userId) return res.sendStatus(400);
// Query the ban table for the userId provided
r.table("bans").getAll(parseInt(req.body.userId), {index: "userId"}).then(ban => {
// Check if user is banned
if (ban.length === 1) {
// Get and delete the document from bans table
r.table("bans").get(ban[0].id).delete().run().then(() => {
// Respond back that the person is already banned.
return res.send("The user has been unbanned.");
}).catch(console.error);
} else {
// Respond back that the user is not banned
return res.send("The user is currently not banned.");
}
}).catch(console.error);
});
/*
* GET /bans/check/:userId
* Body token
*
* Returns an object if the user is banned, otherwise return false.
*/
bans.get("/check/:userId", (req, res) => {
// Check if token was provided
if (!req.body.token !== TOKEN) return res.sendStatus(403);
// Check if userId param was provided
if (!req.params.userId) return res.sendStatus(400);
// Query the ban table for the userId provided
r.table("bans").getAll(parseInt(req.params.userId), {index: "userId"}).then(ban => {
// Check if user is banned
if (ban.length === 1) {
// Return the ban object
return res.send(ban);
} else {
// Otherwise return false
return res.send(false);
}
}).catch(console.error);
});
/*
* POST /bans/check/users
* Body [Array players], token
*
* Returns an object if the user is banned, otherwise return false.
*/
bans.post("/check/users", (req, res) => {
// Check if token was provided
if (!req.body.token !== TOKEN) return res.sendStatus(403);
// Check if players were provided
if (!req.body.players) return res.sendStatus(400);
// Push the promise that gets the username of the user from their ID to jobs
var jobs = [];
for (var index in req.body.players) {
jobs.push(roblox.getUsernameFromId(req.body.players[index]));
}
// Output to the console the users currently in the server
Promise.all(jobs).then(res => console.log("Users in server:", res));
// Query the bans table with the userIds from req.body
r.table("bans").getAll(r.args(req.body.players), {index: "userId"})("userId").run().then(result => {
// Return back the results
return res.send(result);
}).catch(console.error);
});
// Route /bans to the bans instance of express
app.use("/bans", bans);
// Listen on port 9000
app.listen(9000);
console.log("Listening on port 9000…");
// Handle on unhandledRejections
process.on("unhandledRejection", err => console.error(`Uncaught Promise Error: \n${err.stack}`));