-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
377 lines (319 loc) · 13.5 KB
/
main.ts
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { Application, Router, Response, Cookies, send, Status } from "./deps.ts"
const jsonFile = await Deno.readFile("./config.json");
const config = JSON.parse(new TextDecoder().decode(jsonFile));
const { bot_id, token, discord_token, oauth_red, oauth_red_url, verification } = config;
// Access nested properties separately
const { guildID, guildIcon } = verification;
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const app = new Application();
const router = new Router();
const DEBUG = false; // set to true to enable debug mode
const DISCORD_API = "https://discord.com/api/";
const DISCORD_CDN = "https://cdn.discordapp.com/";
const OAUTH_REDIRECT_URL = DEBUG ? "http://localhost:8000/auth" : `${oauth_red}`;
const OAUTH_REDIRECT = DEBUG ? "http%3A%2F%2Flocalhost%3A8000%2Fauth" : `${oauth_red_url}`;
const OAUTH_AUTH = `oauth2/authorize?client_id=${bot_id}&redirect_uri=${OAUTH_REDIRECT}&response_type=code&scope=identify%20guilds`
const OAUTH_TOKEN = "oauth2/token";
const GUILD_INFO = {
id: guildID,
icon: guildIcon
};
const digitRegex = /^\d+$/;
const restrictedRegex = /(server|student|scu faculty\/staff|@everyone|\*|admin|moderator|bots|scu bot > &help|prospective student|----)/i;
const identityRegex = /^(he\/him|she\/her|they\/them|any pronouns|ask for pronouns)/i;
const memberRegex = /^(alumni|grad|freshman|sophomore|junior|senior)/i;
const concentrationRegex = /^(aimes|asian sts|african american sts|pre-health sci|pre-law|pre-teaching|pastoral ministries|musical theatre|urban edu|catholic sts|med & ren sts|latin american sts|latina\/o\/x sts|actg|actg\/is|aero eng|ancient sts|anth|arabic|applied math|arth|biochem|bioe|biol|coen|busn anlyts|chem|chst|chinese|civil eng|clas|comms|csci|couns|couns psyc|econ \(lsb\)|econ \(cas\)|ecen|educ ldrsp|elen|eng mgmt|eng mgmt & ldrsp|eng phys|english|envr sci|envr sts|entr|ethn|fnce|fnc & anlyts|french|gen eng|german|greek lang\/lit|indv sts|info sys|ital|japn|j\.d\.|j\.d\.\/mba|j\.d\.\/msis|latin\/greek|latin lang\/lit|ll\.m\. u\.s\. law|mgmt|mgmt\/entr|hist|ll\.m\. intel property|ll\.m\. intl & comp law|mis|mktg|math|mech eng|mba|mils|music|neur|online mktg|phil|phys|poli sci|power sys & sust nrg|psyc|phsc|real estate|rels|retail|soci|spanish|studio art|sust food sys|teaching cred \(mattc\)|theatre\/dance|und busn|und arts|und eng|wde|wgst)$/i;
const rlcRegex = /^(alpha|campisi|cura|cyphi|da vinci|modern perspectives|loyola|neighborhood units|nobili|sanfilippo|unity|university villas)/i;
const locationRegex = /^(bay area|rocky mountains|northeast|southeast|midwest|southwest|pacific|international)/i;
const othertagsRegex = /^(commuter|residential|community facilitator|club leader|orientation leader|peer advisor|school employee|ukraine)/i;
const regexArray = [restrictedRegex, memberRegex, concentrationRegex, rlcRegex, locationRegex, identityRegex, othertagsRegex];
const categoryArray = ["restricted", "member", "concentration", "rlc", "location", "identity", "tags"];
interface AccessToken {
access_token: string,
token_type: string,
expires_in: number,
refresh_token: string,
scope: string
};
interface User {
id: string,
username: string,
avatar: string,
discriminator: string,
inCorrectGuild: boolean
};
interface Role {
id: string,
name: string,
color: number,
priority: number,
category: string | undefined
};
interface Guild {
id: string,
name: string,
icon: string,
owner: false,
permissions: number,
features: string[],
permissionsNew: string
};
function determineRoleCategory(name: string): string {
for (const index in regexArray) {
if (regexArray[index].test(name)) {
return categoryArray[index]
}
};
return ""; // returns an empty string if there is no role
};
async function getRoles() {
// requires Bot authorization
const response = await fetch(DISCORD_API + "guilds/" + GUILD_INFO.id + "/roles", {
headers: {
"Authorization": "Bot " + `${token}`
}
});
// remove unnecessary role metadata
const json = await response.json();
return json.map((item: any) => {
return {
id: item.id,
name: item.name,
color: item.color,
priority: item.position,
category: determineRoleCategory(item.name)
};
});
};
/**
*
* @return string
* @param cookies
* @param response
*/
async function getIdentity(cookies: Cookies, response: Response) {
const accessToken = await cookies.get("discord-access-token") ?? "";
const identityResponse = await fetch(DISCORD_API + "users/@me", {
headers: {
"Authorization": "Bearer " + accessToken
}
});
if (!identityResponse.ok) {
if (identityResponse.status === 401) {
response.status = Status.Unauthorized;
response.redirect("/bad-auth.html?error=invalid_token");
} else {
response.status = Status.InternalServerError;
response.redirect("/bad-auth.html?error=server_error");
}
return "";
};
const guildsResponse = await fetch(DISCORD_API + "users/@me/guilds", {
headers: {
"Authorization": "Bearer " + accessToken
}
});
if ( !guildsResponse.ok && guildsResponse.status === 401) {
response.status = Status.Unauthorized
response.redirect("/bad-auth.html?error=invalid_token");
return "";
};
const userInfo = await identityResponse.json();
const guildInfo: Guild[] = await guildsResponse.json();
const inCorrectGuild = (guildsArray: Guild[], guildID: string) => {
return guildsArray.filter((guild) => guild["id"] === guildID).length > 0
};
const user: User = {
id: userInfo.id,
username: userInfo.username,
avatar: userInfo.avatar,
discriminator: userInfo.discriminator,
inCorrectGuild: inCorrectGuild(guildInfo, GUILD_INFO.id)
} ;
return JSON.stringify(user);
};
router
.get("/login", (ctx) => {
ctx.response.redirect(DISCORD_API + OAUTH_AUTH)
})
.get("/auth", async (ctx) => {
// parse response from Discord API authorization (30 char alphanumerical)
const regex = /^[A-Za-z0-9]{30}$/
const code = ctx.request.url.searchParams.get("code");
console.log("Received authorization code:", code);
const check = regex.test(code)
// authorization code is bad
if (!check) {
console.log("Invalid authorization code. Redirecting to /bad-auth.html");
ctx.response.redirect("/bad-auth.html")
return
}
const data = new URLSearchParams({
client_id: `${bot_id}`,
client_secret: `${discord_token}`,
grant_type: "authorization_code",
code: code,
redirect_uri: OAUTH_REDIRECT_URL,
scope: "identify email guilds"
})
const headers = {
"Content-Type": "application/x-www-form-urlencoded"
};
// exchange authorization grant for access token
try {
const result = await fetch(DISCORD_API + OAUTH_TOKEN, {
method: "POST",
body: data,
headers: headers
});
const accessToken: AccessToken = await result.json();
console.log("Access Token: " + accessToken.access_token + " " + accessToken.expires_in)
if (regex.test(accessToken.access_token)) {
await ctx.cookies.set("discord-access-token", accessToken.access_token);
await ctx.cookies.set("discord-token-expiration", Date.now().toString()) // todo cookie math
console.log("User authenticated successfully. Redirecting to dashboard.html...");
ctx.response.redirect("/dashboard.html");
} else {
console.log("Invalid access token received. Redirecting to /bad-auth.html");
ctx.response.status = Status.BadRequest
ctx.response.redirect("/bad-auth.html")
return
}
} catch (error) {
console.error("Error fetching access token:", error);
console.log("Redirecting to /bad-auth.html due to an error.");
ctx.response.status = Status.InternalServerError;
ctx.response.redirect("/bad-auth.html");
return;
}
})
.post("/identity", async (ctx) => {
ctx.response.body = await getIdentity(ctx.cookies, ctx.response)
})
.get("/images/:path*", (ctx) => {
if (ctx.params && ctx.params.path) {
ctx.response.body = DISCORD_CDN + ctx.params.path;
};
})
.get("/userroles/:userid", async (ctx) => {
if (ctx.params && ctx.params.userid) {
// verify that the userid is a number
if (!digitRegex.test(ctx.params.userid)) {
ctx.response.status = Status.BadRequest
return
}
console.log("Fetching user roles: " + ctx.params.userid)
const response = await fetch(DISCORD_API + "guilds/" + GUILD_INFO.id + "/members/" + ctx.params.userid, {
headers: {
"Authorization": "Bot " + `${token}`
}
});
// todo need to verify identity?
ctx.response.body = await response.json();
}
})
.post("/roles", async (ctx) => {
ctx.response.body = await getRoles();
})
.post("/save", async ctx => {
interface SavePayload {
userID: string,
rolesToAdd: string[],
rolesToRemove: string[]
};
// Grab latest copy of all roles
const roles = await getRoles();
const payload = ctx.request.body()
if (payload.type === "json") {
const savePayload: SavePayload = await payload.value;
// verify identity (make sure user is properly authenticated)
const identityResponse = await getIdentity(ctx.cookies, ctx.response);
if (identityResponse === "") {
ctx.response.status = Status.Unauthorized
ctx.response.redirect("/bad-auth.html")
return
};
const identity = JSON.parse(identityResponse).id;
if (identity !== savePayload.userID) {
ctx.response.status = Status.Unauthorized
return
};
// sanitize roles (remove restricted roles)
savePayload.rolesToAdd = savePayload.rolesToAdd.filter((roleID) => {
return digitRegex.test(roleID) && !roles.some((role: Role) => role.category === "restricted" && role.id === roleID)
});
savePayload.rolesToRemove = savePayload.rolesToRemove.filter((roleID) => {
return digitRegex.test(roleID) && !roles.some((role: Role) => role.category === "restricted" && role.id === roleID)
});
if (savePayload.rolesToAdd.length === 0 && savePayload.rolesToRemove.length === 0) {
ctx.response.status = Status.UnprocessableEntity
return
};
const roleAPI = `guilds/${GUILD_INFO.id}/members/${savePayload.userID}/roles/`; // /{role.id}
// assign roles
for (const roleID of savePayload.rolesToAdd) {
await wait(1000);
const options = {
headers: {
"Authorization": "Bot " + `${token}`,
"Content-Length": "0"
},
method: "PUT",
body: null
};
fetch(DISCORD_API + roleAPI + roleID, options)
.then((res) => {
console.log(res.status)
res.text().then(console.log)
if (res.status === 429) {
// rate limited
};
})
.catch((err) => {
console.error(err)
ctx.response.status = Status.ServiceUnavailable
return
})
}
// remove roles
for (const roleID of savePayload.rolesToRemove) {
await wait(1000)
fetch(DISCORD_API + roleAPI + roleID, {
headers: {
"Authorization": "Bot " + `${token}`
},
method: "DELETE"
}).then((res) => {
console.log(res.status)
console.log(res.body)
if (res.status === 429) {
// rate limited
}
}).catch((err) => {
console.error(err)
ctx.response.status = Status.ServiceUnavailable
return
})
}
// console.log("SAVE: " + payload)
ctx.response.status = Status.OK;
} else {
console.error("Bad payload received. " + payload.type)
ctx.response.status = Status.UnprocessableEntity
};
})
.get("/logout", (ctx) => {
ctx.cookies.delete("discord-access-token")
ctx.response.redirect("/")
});
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async (ctx) => {
ctx.response.headers.set("Cache-Control", "max-age=604800")
await send(ctx, ctx.request.url.pathname, {
root: DEBUG ? `${Deno.cwd()}/static` : "/home/scu-discord-bot/static",
index: "index.html",
});
});
console.log(`🦕 Deno server running at http://localhost:8000/ 🦕`)
await app.listen({ port: 8000 });