diff --git a/package.json b/package.json index 27a70dd..650c659 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.ts", "type": "module", "scripts": { - "dev": "prisma migrate deploy & node --no-warnings --import=tsx --env-file=.env --watch src/index.ts --watch src/swaggerDocument.yml" + "dev": "prisma migrate deploy & node --no-warnings --import=tsx --env-file=.env --watch src/index.ts" }, "author": "Sebastian 'Moonded' Danielzik", "license": "ISC", diff --git a/src/api/bot/commands/quotes.ts b/src/api/bot/commands/quotes.ts index 3becc35..e176684 100644 --- a/src/api/bot/commands/quotes.ts +++ b/src/api/bot/commands/quotes.ts @@ -4,16 +4,20 @@ import { prisma, getLatestRelease } from "utils"; const router = express.Router(); router.post("/", async (req, res) => { - const data = req.body as { quote: string; responder: string }; - try { - const quote = await prisma.quotes.create({ - data: { - quote: data.quote, - responder: data.responder, - }, - }); - return res.send(quote); + const data = req.body as { quote: string; responder: string }; + + try { + const quote = await prisma.quotes.create({ + data: { + quote: data.quote, + responder: data.responder, + }, + }); + return res.send(quote); + } catch (e) { + return res.sendStatus(500); + } } catch (e) { return res.sendStatus(500); } diff --git a/src/api/bot/commands/trivia.ts b/src/api/bot/commands/trivia.ts index 12fa876..824d5a0 100644 --- a/src/api/bot/commands/trivia.ts +++ b/src/api/bot/commands/trivia.ts @@ -4,19 +4,23 @@ import { prisma } from "utils"; const router = express.Router(); router.post("/", async (req, res) => { - const data = req.body as { - question: string; - answer: string; - }; - try { - const newTrivia = await prisma.trivia.create({ - data: { - question: data.question, - answer: data.answer, - }, - }); - return res.send(newTrivia); + const data = req.body as { + question: string; + answer: string; + }; + + try { + const newTrivia = await prisma.trivia.create({ + data: { + question: data.question, + answer: data.answer, + }, + }); + return res.send(newTrivia); + } catch (e) { + return res.sendStatus(500); + } } catch (e) { return res.sendStatus(500); } diff --git a/src/api/bot/commands/user.ts b/src/api/bot/commands/user.ts index 5518465..8e7ff32 100644 --- a/src/api/bot/commands/user.ts +++ b/src/api/bot/commands/user.ts @@ -4,47 +4,51 @@ import { prisma } from "utils"; const router = express.Router(); router.put("/", async (req, res) => { - const data = req.body as { - user: string; - id: string; - nexusmods: string; - github: string; - theme: string; - description: string; - style: string; - timezone: string; - country: string; - }; - try { - const user = await prisma.user.upsert({ - where: { - userid: data.id, - }, - update: { - userid: data.id, - user: data.user, - nexusmods: data.nexusmods, - github: data.github, - theme: data.theme, - description: data.description, - style: data.style, - timezone: data.timezone, - country: data.country, - }, - create: { - userid: data.id, - user: data.user, - nexusmods: data.nexusmods, - github: data.github, - theme: data.theme, - description: data.description, - style: data.style, - timezone: data.timezone, - country: data.country, - }, - }); - return res.send(user); + const data = req.body as { + user: string; + id: string; + nexusmods: string; + github: string; + theme: string; + description: string; + style: string; + timezone: string; + country: string; + }; + + try { + const user = await prisma.user.upsert({ + where: { + userid: data.id, + }, + update: { + userid: data.id, + user: data.user, + nexusmods: data.nexusmods, + github: data.github, + theme: data.theme, + description: data.description, + style: data.style, + timezone: data.timezone, + country: data.country, + }, + create: { + userid: data.id, + user: data.user, + nexusmods: data.nexusmods, + github: data.github, + theme: data.theme, + description: data.description, + style: data.style, + timezone: data.timezone, + country: data.country, + }, + }); + return res.send(user); + } catch (e) { + return res.sendStatus(500); + } } catch (e) { return res.sendStatus(500); } diff --git a/src/api/bot/dev/users.ts b/src/api/bot/dev/users.ts index eb6da27..a86cc77 100644 --- a/src/api/bot/dev/users.ts +++ b/src/api/bot/dev/users.ts @@ -3,29 +3,33 @@ import { prisma } from "utils"; const router = express.Router(); router.get("/", async (req, res) => { - const data = req.query.q as { - id: string; - }; + try { + const data = req.query.q as { + id: string; + }; - console.log(data); + console.log(data); - if (!data) { - // FIND ALL USERS - const User = await prisma.user.findMany(); + if (!data) { + // FIND ALL USERS + const User = await prisma.user.findMany(); - return res.send(User); - } else { - // FIND USER BY ID - try { - const User = await prisma.user.findUnique({ - where: { - userid: data.id, - }, - }); return res.send(User); - } catch (e) { - return res.sendStatus(500); + } else { + // FIND USER BY ID + try { + const User = await prisma.user.findUnique({ + where: { + userid: data.id, + }, + }); + return res.send(User); + } catch (e) { + return res.sendStatus(500); + } } + } catch (e) { + return res.sendStatus(500); } }); diff --git a/src/api/bot/team/pass.ts b/src/api/bot/team/pass.ts index c4f04cd..6253a20 100644 --- a/src/api/bot/team/pass.ts +++ b/src/api/bot/team/pass.ts @@ -7,48 +7,89 @@ import crypto from "node:crypto"; const router = express.Router(); router.post("/create", async (req, res) => { - const data = req.body as { - username: string; - password: string; - discord: string; - }; + try { + const data = req.body as { + username: string; + password: string; + discord: string; + }; + + const salt = crypto.randomBytes(16).toString("base64"); + const tokens = sha512(data.password + salt); - const salt = crypto.randomBytes(16).toString("base64"); - const tokens = sha512(data.password + salt); + const token = jwt.sign(data, process.env.API_TOKEN as string); - const token = jwt.sign(data, process.env.API_TOKEN as string); + const user = await prisma.pass.findUnique({ + where: { + username: data.username, + }, + }); - const user = await prisma.pass.findUnique({ - where: { - username: data.username, - }, - }); + if (user) { + return res.status(403).send("User already exists"); + } - if (user) { - return res.status(403).send("User already exists"); - } + const pass = await prisma.pass.create({ + data: { + username: data.username, + discord: data.discord, + token: tokens, + salt: salt, + jwt: token, + }, + }); - const pass = await prisma.pass.create({ - data: { - username: data.username, - discord: data.discord, - token: tokens, - salt: salt, - jwt: token, - }, - }); - - return res.send(token); + return res.send(token); + } catch (e) { + return res.sendStatus(500); + } }); router.post("/key", async (req, res) => { - const data = req.body as { - username?: string; - password: string; - discord?: string; - }; + try { + const data = req.body as { + username?: string; + password: string; + discord?: string; + }; + + try { + const user = await prisma.pass.findUnique({ + where: { + username: data.username, + discord: data.discord, + }, + }); + + if (!user) { + return res.status(403).send("User not found"); + } + + const tokens = sha512(data.password + user.salt); + + if (tokens !== user.token) { + return res.status(403).send("Invalid password"); + } + + return res.send(user.jwt); + } catch (e) { + return res.sendStatus(500); + } + } catch (e) { + return res.sendStatus(500); + } +}); +router.post("/update", async (req, res) => { try { + const data = req.body as { + username?: string; + password: string; + discord?: string; + }; + + console.log(data); + const user = await prisma.pass.findUnique({ where: { username: data.username, @@ -66,58 +107,27 @@ router.post("/key", async (req, res) => { return res.status(403).send("Invalid password"); } - return res.send(user.jwt); - } catch (e) { - return res.sendStatus(500); - } -}); - -router.post("/update", async (req, res) => { - const data = req.body as { - username?: string; - password: string; - discord?: string; - }; - - console.log(data) - - const user = await prisma.pass.findUnique({ - where: { - username: data.username, - discord: data.discord, - }, - }); + const salt = crypto.randomBytes(16).toString("base64"); + const newTokens = sha512(data.password + salt); + const token = jwt.sign(data, process.env.API_TOKEN as string); - if (!user) { - return res.status(403).send("User not found"); - } - - const tokens = sha512(data.password + user.salt); + const updated = await prisma.pass.update({ + where: { + username: data.username, + discord: data.discord, + }, + data: { + token: newTokens, + salt: salt, + jwt: token, + }, + }); - if (tokens !== user.token) { - return res.status(403).send("Invalid password"); + return res.send(token); + } catch (e) { + return res.sendStatus(500); } - - const salt = crypto.randomBytes(16).toString("base64"); - const newTokens = sha512(data.password + salt); - - const token = jwt.sign(data, process.env.API_TOKEN as string); - - const updated = await prisma.pass.update({ - where: { - username: data.username, - discord: data.discord, - }, - data: { - token: newTokens, - salt: salt, - jwt: token, - }, - }); - - - return res.send(token); }); export default router; diff --git a/src/api/discord/util/bans.ts b/src/api/discord/util/bans.ts index 1296d25..79ac58e 100644 --- a/src/api/discord/util/bans.ts +++ b/src/api/discord/util/bans.ts @@ -3,14 +3,19 @@ import { log, client } from "utils"; const router = express.Router(); router.get("/", async (req, res) => { - if (!req.query.server || typeof req.query.server !== "string") { - return res.status(400).send("Invalid query"); - } + try { + if (!req.query.server || typeof req.query.server !== "string") { + return res.status(400).send("Invalid query"); + } - const bans = await client.guilds.cache.get(req.query.server)?.bans.fetch() - res.send(bans); + const bans = await client.guilds.cache.get(req.query.server)?.bans.fetch(); - log("Roles sent"); + log("Roles sent"); + return res.send(bans); + } catch (e) { + log(e); + return res.sendStatus(500); + } }); export default router; diff --git a/src/api/discord/util/channels.ts b/src/api/discord/util/channels.ts index 2dead51..68e5354 100644 --- a/src/api/discord/util/channels.ts +++ b/src/api/discord/util/channels.ts @@ -3,17 +3,21 @@ import { log, client } from "utils"; const router = express.Router(); router.get("/", async (req, res) => { - if (!req.query.server || typeof req.query.server !== "string") { - return res.status(400).send("Invalid query"); - } - - const channels = await client.guilds.cache - .get(req.query.server) - ?.channels.fetch(); + try { + if (!req.query.server || typeof req.query.server !== "string") { + return res.status(400).send("Invalid query"); + } - res.send(channels); + const channels = await client.guilds.cache + .get(req.query.server) + ?.channels.fetch(); - log("Roles sent"); + log("Roles sent"); + return res.send(channels); + } catch (e) { + log(e); + return res.sendStatus(500); + } }); export default router; diff --git a/src/api/discord/util/roles.ts b/src/api/discord/util/roles.ts index 5d92184..09cadbc 100644 --- a/src/api/discord/util/roles.ts +++ b/src/api/discord/util/roles.ts @@ -14,35 +14,39 @@ const gauge_count = new prm_client.Gauge({ }); router.get("/", (req, res) => { - if (!req.query.server || typeof req.query.server !== "string") { - return res.status(400).send("Invalid query"); - } - gauge_time.setToCurrentTime(); - const end = gauge_time.startTimer(); - - const send = [ - client.guilds.cache - .get(req.query.server.trim()) - ?.roles.cache.map((role) => { - return { - Role: role.name, - Members: role.members.map((member) => { - return { - Username: member.user.username, - Discriminator: member.user.discriminator, - ID: member.user.id, - Bot: member.user.bot, - }; - }), - }; - }), - ]; + try { + if (!req.query.server || typeof req.query.server !== "string") { + return res.status(400).send("Invalid query"); + } + gauge_time.setToCurrentTime(); + const end = gauge_time.startTimer(); - res.send(send); - end(); - gauge_count.inc(1); + const send = [ + client.guilds.cache + .get(req.query.server.trim()) + ?.roles.cache.map((role) => { + return { + Role: role.name, + Members: role.members.map((member) => { + return { + Username: member.user.username, + Discriminator: member.user.discriminator, + ID: member.user.id, + Bot: member.user.bot, + }; + }), + }; + }), + ]; + end(); + gauge_count.inc(1); - log("Roles sent"); + log("Roles sent"); + return res.send(send); + } catch (e) { + log(e); + return res.sendStatus(500); + } }); export default router; diff --git a/src/api/discord/util/server.ts b/src/api/discord/util/server.ts index ec8af0e..eb4dbba 100644 --- a/src/api/discord/util/server.ts +++ b/src/api/discord/util/server.ts @@ -3,15 +3,19 @@ import { log, client } from "utils"; const router = express.Router(); router.get("/", async (req, res) => { - if (!req.query.q || typeof req.query.q !== "string") { - return res.status(400).send("Invalid query"); - } - - const servers = await client.guilds.fetch(req.query.q); + try { + if (!req.query.q || typeof req.query.q !== "string") { + return res.status(400).send("Invalid query"); + } - res.send(servers); + const servers = await client.guilds.fetch(req.query.q); - log("Server sent"); + log("Server sent"); + return res.send(servers); + } catch (e) { + log(e); + return res.sendStatus(500); + } }); export default router; diff --git a/src/api/discord/util/user.ts b/src/api/discord/util/user.ts index f58861a..6cb1d61 100644 --- a/src/api/discord/util/user.ts +++ b/src/api/discord/util/user.ts @@ -79,33 +79,38 @@ const gauge_count_detail = new prm_client.Gauge({ }); router.post("/details", async (req, res) => { - if (!req.query.server || typeof req.query.server !== "string") { - return res.status(400).send("Invalid query"); - } - gauge_time_detail.setToCurrentTime(); - const end_detail = gauge_time_user.startTimer(); - const UserArray = req.body as string[]; + try { + if (!req.query.server || typeof req.query.server !== "string") { + return res.status(400).send("Invalid query"); + } + gauge_time_detail.setToCurrentTime(); + const end_detail = gauge_time_user.startTimer(); + const UserArray = req.body as string[]; - if ( - !UserArray || - !Array.isArray(UserArray) || - UserArray.length === 0 || - UserArray.some((user) => typeof user !== "string") - ) { - return res.status(400).send("Invalid query"); - } + if ( + !UserArray || + !Array.isArray(UserArray) || + UserArray.length === 0 || + UserArray.some((user) => typeof user !== "string") + ) { + return res.status(400).send("Invalid query"); + } - const Users = await prisma.user.findMany({ - where: { - userid: { - in: UserArray, + const Users = await prisma.user.findMany({ + where: { + userid: { + in: UserArray, + }, }, - }, - }); + }); - res.send(Users); - end_detail(); - gauge_count_detail.inc(1); + end_detail(); + gauge_count_detail.inc(1); + return res.send(Users); + } catch (e) { + log(e); + return res.sendStatus(500); + } }); export default router; diff --git a/src/api/discord/web/index.ts b/src/api/discord/web/index.ts index 6650db0..0164dee 100644 --- a/src/api/discord/web/index.ts +++ b/src/api/discord/web/index.ts @@ -2,54 +2,62 @@ import express from "express"; import { prisma, log, client } from "utils"; const router = express.Router(); -type acc = { - Username: string; - Nickname: string | null; - Image: string; - ID: string; - Bot: boolean; - Roles: { - Role: string; - ID: string; - Icon: string | null; - Position: number; - }[]; - CustomData: {} | null; -}[]; - router.get("/", async (req, res) => { - if (!req.query.q || typeof req.query.q !== "string") { - return res.status(400).send("Invalid query"); - } + try { + if (!req.query.q || typeof req.query.q !== "string") { + return res.status(400).send("Invalid query"); + } - const Members = await client.guilds.cache - .get(req.query.q.trim()) - ?.members.fetch(); + const Members = await client.guilds.cache + .get(req.query.q.trim()) + ?.members.fetch(); - const Users = await prisma.user.findMany(); + const Users = await prisma.user.findMany(); - const data = Members?.reduce((acc: acc, member) => { - acc.push({ - Username: member.user.username, - Nickname: member.nickname, - Image: member.user.displayAvatarURL(), - ID: member.user.id, - Bot: member.user.bot, - Roles: member.roles.cache.map((role) => { - return { - Role: role.name, - ID: role.id, - Icon: role.iconURL(), - Position: role.position, - }; - }), - CustomData: Users.find((a) => a.userid === member.user.id) || null, - }); - return acc; - }); - - log("Experimental Roles sent"); - return res.send(data); + const data = Members?.reduce( + ( + acc: { + Username: string; + Nickname: string | null; + Image: string; + ID: string; + Bot: boolean; + Roles: { + Role: string; + ID: string; + Icon: string | null; + Position: number; + }[]; + CustomData: {} | null; + }[], + member + ) => { + acc.push({ + Username: member.user.username, + Nickname: member.nickname, + Image: member.user.displayAvatarURL(), + ID: member.user.id, + Bot: member.user.bot, + Roles: member.roles.cache.map((role) => { + return { + Role: role.name, + ID: role.id, + Icon: role.iconURL(), + Position: role.position, + }; + }), + CustomData: Users.find((a) => a.userid === member.user.id) || null, + }); + return acc; + } + ); + + log("Experimental Roles sent"); + return res.send(data); + } catch (e) { + log(e); + return res.sendStatus(500); + } }); export default router; diff --git a/src/api/endpoint/response.ts b/src/api/endpoint/response.ts index 5c17a68..ce1fe7d 100644 --- a/src/api/endpoint/response.ts +++ b/src/api/endpoint/response.ts @@ -3,62 +3,80 @@ import { prisma } from "utils"; const router = express.Router(); router.get("/", async (req, res) => { - const data = (await prisma.quotes.findMany()) || [{ error: "No data found" }]; - - res.send(data); + try { + const data = (await prisma.quotes.findMany()) || [ + { error: "No data found" }, + ]; + + return res.send(data); + } catch (e) { + return res.sendStatus(500); + } }); router.post("/", async (req, res) => { - const data = req.body as { quote: string; responder: string }; - - if (!data.responder) { - return res.status(400).send("No responder provided"); + try { + const data = req.body as { quote: string; responder: string }; + + if (!data.responder) { + return res.status(400).send("No responder provided"); + } + + if (!data) { + return res.status(400).send("No data provided"); + } + + const quote = await prisma.quotes.create({ + data: { + quote: data.quote, + responder: data.responder, + }, + }); + + return res.send(quote); + } catch (e) { + return res.sendStatus(500); } - - if (!data) { - return res.status(400).send("No data provided"); - } - - const quote = await prisma.quotes.create({ - data: { - quote: data.quote, - responder: data.responder, - }, - }); - - res.send(quote); }); router.put("/", async (req, res) => { - const data = req.body as [{ quote: string; responder: string }]; + try { + const data = req.body as [{ quote: string; responder: string }]; - if (!data) { - return res.status(400).send("No data provided"); - } + if (!data) { + return res.status(400).send("No data provided"); + } - const quote = await prisma.quotes.createMany({ - data: data, - }); + const quote = await prisma.quotes.createMany({ + data: data, + }); - res.send(quote); + return res.send(quote); + } catch (e) { + return res.sendStatus(500); + } }); router.patch("/", async (req, res) => { - const data = req.body as { id: string; quote: string; responder: string }; - - if (!data.id) { - return res.status(400).send("No id provided"); + try { + const data = req.body as { id: string; quote: string; responder: string }; + + if (!data.id) { + return res.status(400).send("No id provided"); + } + + const quote = await prisma.quotes.update({ + where: { id: data.id }, + data: { + quote: data.quote, + responder: data.responder, + }, + }); + + return res.send(quote); + } catch (e) { + return res.sendStatus(500); } - - const quote = await prisma.quotes.update({ - where: { id: data.id }, - data: { - quote: data.quote, - responder: data.responder, - }, - }); - - res.send(quote); }); export default router; diff --git a/src/index.ts b/src/index.ts index db7dd85..f37ee5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ const credentials = { key: privateKey, cert: certificate }; app.get("/", (rq, rs) => { rs.redirect("/api"); }); + import swaggerUi from "swagger-ui-express"; const swaggerDocument = yaml.parse(