Skip to content

Commit

Permalink
update and fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonded committed Aug 27, 2024
1 parent c593ee2 commit 85f6878
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 85 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"main": "index.ts",
"type": "module",
"scripts": {
"dev": "prisma migrate deploy & node --no-warnings --import=tsx --env-file=.env --watch src/index.ts"
"dev": "prisma migrate deploy & node --no-warnings --import=tsx --env-file=.env --watch src/index.ts --watch src/swaggerDocument.yml"
},
"author": "Sebastian 'Moonded' Danielzik",
"license": "ISC",
"dependencies": {
"@prisma/client": "5.18.0",
"@types/swagger-ui-express": "^4.1.6",
"discord.js": "^14.15.3",
"express": "^4.19.2",
"express-rate-limit": "^7.4.0",
Expand All @@ -19,8 +20,10 @@
"octokit": "^4.0.2",
"prisma": "^5.18.0",
"prom-client": "^15.1.3",
"swagger-ui-express": "^5.0.1",
"tsx": "^4.17.0",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"yaml": "^2.5.0"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
40 changes: 40 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 48 additions & 2 deletions src/api/discord/web/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
import express from "express";
import { prisma, log, client } from "utils";
const router = express.Router();

import web from "./web";
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.use("/web", web);
router.get("/", async (req, res) => {
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 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);
});

export default router;
73 changes: 0 additions & 73 deletions src/api/discord/web/web.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ router.use("/bot", apiToken, bot);
// router.use("/dev", apiToken, dev);
// router.use("/auth", apiToken, auth);

router.get("/", (req, res) => {
res.sendStatus(200);
});

export default router;
22 changes: 18 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import https from "https";
import rateLimit from "express-rate-limit";

import fs from "fs";

import yaml from "yaml";
import jwt from "jsonwebtoken";

const admin = jwt.sign({ username: "admin" }, process.env.API_TOKEN as string);
Expand All @@ -21,9 +21,23 @@ const certificate = fs.readFileSync("./src/keys/fullchain.pem", "utf-8");

const credentials = { key: privateKey, cert: certificate };

app.get("/", (rq, rs) => {
rs.redirect("/api");
});
// app.get("/", (rq, rs) => {
// rs.redirect("/api");
// });
import swaggerUi from "swagger-ui-express";

const swaggerDocument = yaml.parse(
fs.readFileSync("./src/swaggerDocument.yml", "utf-8")
);


const options = {
swaggerOptions: {
supportedSubmitMethods: []
},
};

app.use("/", swaggerUi.serve, swaggerUi.setup(swaggerDocument, options));

const windowMS = Number(process.env.API_RATE_WINDOW) * 1000 || 60 * 1000;
const requests = Number(process.env.API_MAX_REQUESTS) || 10;
Expand Down
79 changes: 79 additions & 0 deletions src/swaggerDocument.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
openapi: "3.0.0"
info:
title: "RED Modding Tools Backend Api"
description: "This is the RED Modding Tools Backend Api documentation"
version: "0.2.0"

servers:
- url: "http://localhost:3000/api"
description: "Local server"

paths:
/bot/commands/core-versions:
get:
summary: Github Core Plugin Versions
tags:
- Bot

/bot/commands/core-versions/update:
get:
summary: Manuel Updater for Core Plugin Versions
tags:
- Bot
/bot/commands/quotes:
get:
summary: Bot.
description: Quotes
tags:
- Bot
/bot/commands/respond:
get:
summary: Bot.
description: Responses
tags:
- Bot
/bot/commands/trivia:
get:
summary: Bot.
description: Trivia
tags:
- Bot
/bot/commands/user:
get:
summary: Bot.
description: Quotes
tags:
- Bot
/bot/dev/user:
get:
summary: Bot.
description: Quotes
tags:
- Bot
/bot/team/pass:
get:
summary: Bot.
description: Quotes
tags:
- Bot

/discord:
get:
summary: Discord.
description: Get the Discord invite link.
tags:
- Discord

/endpoint:
get:
summary: Endpoint.
description: Get the endpoint.
tags:
- Endpoint

/metrics:
get:
summary: Prometheus Metrics.
description: Get the Prometheus metrics.
tags:
- Metrics

0 comments on commit 85f6878

Please sign in to comment.