-
Notifications
You must be signed in to change notification settings - Fork 32
/
server.js
269 lines (243 loc) · 6.49 KB
/
server.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
require('dotenv').config()
const Koa = require('koa')
const Router = require('koa-joi-router')
const Joi = Router.Joi
const errorHandler = require('koa-better-error-handler')
const koa404Handler = require('koa-404-handler')
const Boom = require('@hapi/boom')
const bodyParser = require('koa-bodyparser')
const nbx = require('noblox.js')
const app = new Koa()
const router = Router()
const { COOKIE, API_TOKEN, MAX_RANK, PORT } = process.env
app.context.onerror = errorHandler()
app.context.api = true
app.use(koa404Handler)
app.use(bodyParser())
let loggedIn = false
async function authenticate (ctx) {
if (ctx.request.headers.authorization !== API_TOKEN) {
ctx.throw(Boom.unauthorized('Authorization header does not match API_TOKEN.'))
}
}
async function nbxAuthenticate (ctx) {
if (!loggedIn) {
ctx.throw(Boom.unauthorized('You are not logged into a Roblox account, please update COOKIE.'))
}
}
async function throwError (ctx, error) {
const boomResponse = error
ctx.status = boomResponse.output.statusCode
ctx.body = boomResponse.output.payload
}
router.get('/', async (ctx) => {
ctx.status = 200
})
// GET username from ID route
router.route({
method: 'get',
path: '/get-username-from-id/:id',
handler: async (ctx) => {
await nbx.getUsernameFromId(ctx.params.id).then(function (username) {
ctx.body = {
success: true,
data: username
}
}).catch(function (err) {
ctx.throw(Boom.notFound(err))
})
}
})
// GET ID from username route
router.route({
method: 'get',
path: '/get-id-from-username/:username',
handler: async (ctx) => {
await nbx.getIdFromUsername(ctx.params.username).then(function (username) {
ctx.body = {
success: true,
data: username
}
}).catch(function (err) {
ctx.throw(Boom.notFound(err))
})
}
})
// GET a player's information
router.route({
method: 'get',
path: '/user/:id',
pre: async (ctx, next) => {
await authenticate(ctx, next)
return next()
},
handler: async (ctx) => {
if (ctx.invalid) {
await throwError(ctx, Boom.badRequest(ctx.invalid.body))
return
}
return nbx.getPlayerInfo(ctx.params.id).then((playerInfo) => {
ctx.status = 200
ctx.body = playerInfo
}).catch((err) => {
return throwError(ctx, Boom.badRequest(err.message))
})
}
})
// POST a response to a user join request
router.route({
method: 'post',
path: '/group/:group/handle-join-request',
validate: {
type: 'json',
body: Joi.object({
target: Joi.number().required(),
accept: Joi.boolean().required()
}),
continueOnError: true
},
pre: async (ctx, next) => {
await authenticate(ctx, next)
await nbxAuthenticate(ctx, next)
return next()
},
handler: async (ctx) => {
if (ctx.invalid) {
await throwError(ctx, Boom.badRequest(ctx.invalid.body))
return
}
return nbx.handleJoinRequest(ctx.request.params.group, ctx.request.body.target, ctx.request.body.accept).then(() => {
ctx.status = 200
ctx.body = {
success: true,
message: `User's join request was ${ctx.request.body.accept ? 'accepted' : 'declined'} successfully.`
}
}).catch((err) => {
console.log(err)
return throwError(ctx, Boom.unauthorized(err.message))
})
}
})
// POST a group shout
router.route({
method: 'post',
path: '/group/:group/shout',
validate: {
type: 'json',
body: Joi.object({
message: Joi.string().required()
}),
continueOnError: true
},
pre: async (ctx, next) => {
await authenticate(ctx, next)
await nbxAuthenticate(ctx, next)
return next()
},
handler: async (ctx) => {
if (ctx.invalid) {
await throwError(ctx, Boom.badRequest(ctx.invalid.body))
return
}
return nbx.shout(ctx.request.params.group, ctx.request.body.message).then((res) => {
ctx.status = 200
ctx.body = res
}).catch((err) => {
return throwError(ctx, Boom.unauthorized(err.message))
})
}
})
// DELETE a user (exile)
router.route({
method: 'delete',
path: '/group/:group/member/:target',
pre: async (ctx, next) => {
await authenticate(ctx, next)
await nbxAuthenticate(ctx, next)
return next()
},
handler: async (ctx) => {
if (ctx.invalid) {
await throwError(ctx, Boom.badRequest(ctx.invalid.body))
return
}
return nbx.exile(ctx.params.group, ctx.params.target).then(() => {
ctx.status = 200
ctx.body = {
success: true
}
}).catch((err) => {
return throwError(ctx, Boom.badRequest(err.message))
})
}
})
// POST to the rank of a user
router.route({
method: 'post',
path: '/group/:group/member/:target/rank',
validate: {
type: 'json',
body: Joi.object({
rank: Joi.number().min(1).max(Number(MAX_RANK) || 254),
role: Joi.string()
}).xor('rank', 'role'),
continueOnError: true
},
pre: async (ctx, next) => {
await authenticate(ctx, next)
await nbxAuthenticate(ctx, next)
return next()
},
handler: async (ctx) => {
if (ctx.invalid) {
await throwError(ctx, Boom.badRequest(ctx.invalid.body))
return
}
return nbx.setRank(ctx.request.params.group, ctx.request.params.target, ctx.request.body.rank || ctx.request.body.role).then((res) => {
ctx.status = 200
ctx.body = res
}).catch((err) => {
return throwError(ctx, Boom.badRequest(err.message))
})
}
})
// POST to the rank of a user by providing a value to change it by
router.route({
method: 'post',
path: '/group/:group/member/:target/rank-change',
validate: {
type: 'json',
body: Joi.object({
change: Joi.number().required()
}),
continueOnError: true
},
pre: async (ctx, next) => {
await authenticate(ctx, next)
await nbxAuthenticate(ctx, next)
return next()
},
handler: async (ctx) => {
if (ctx.invalid) {
await throwError(ctx, Boom.badRequest(ctx.invalid.body))
return
}
return nbx.changeRank(ctx.request.params.group, ctx.request.params.target, ctx.request.body.change).then((res) => {
ctx.status = 200
ctx.body = res
}).catch((err) => {
return throwError(ctx, Boom.badRequest(err.message))
})
}
})
app.use(router.middleware())
if (COOKIE) {
nbx.setCookie(COOKIE).then((currentUser) => {
loggedIn = true
app.listen(PORT)
console.log(`Listening on port ${PORT},`, `logged into ${currentUser.name}#${currentUser.id}`)
})
} else {
app.listen(PORT)
console.log(`Listening on port ${PORT},`, 'not logged in.')
}