From afd6055519dbc058c39183225463f92cbb1f88ae Mon Sep 17 00:00:00 2001 From: moe-music Date: Thu, 31 Aug 2023 11:50:31 +0530 Subject: [PATCH] update --- src/api/index.ts | 2 +- src/api/middlewares/Middlewares.ts | 1 - src/api/routes/MainRouter.ts | 2 ++ src/api/routes/auth/AuthRouter.ts | 5 +++-- src/api/routes/guilds/GuildRouter.ts | 27 ++++++++++++++++++++++++ src/api/routes/guilds/GuildsRouter.ts | 4 ---- src/api/services/guilds/GuildServices.ts | 12 +++++++++++ src/config.ts | 5 +++-- 8 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 src/api/routes/guilds/GuildRouter.ts diff --git a/src/api/index.ts b/src/api/index.ts index a01cb6630..2c402a89b 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -21,7 +21,7 @@ export class DiscordDashboard { this.app.use(express.urlencoded({ extended: true })); this.app.use(this.errorHandler.bind(this)); this.app.use(cors({ - origin: config.dashboard.website + ':' + config.dashboard.port, + origin: config.dashboard.host + ':' + config.dashboard.port, credentials: true })); this.app.use(session({ diff --git a/src/api/middlewares/Middlewares.ts b/src/api/middlewares/Middlewares.ts index 596b41ffd..429e9cff9 100644 --- a/src/api/middlewares/Middlewares.ts +++ b/src/api/middlewares/Middlewares.ts @@ -1,7 +1,6 @@ import { Request, Response, NextFunction } from 'express'; export const IsAuth = (req: Request, res: Response, next: NextFunction) => { - console.log(`IsAuth: ${req.user}`); if (req.user) { next(); } else { diff --git a/src/api/routes/MainRouter.ts b/src/api/routes/MainRouter.ts index aac6a5955..77e8fb1c6 100644 --- a/src/api/routes/MainRouter.ts +++ b/src/api/routes/MainRouter.ts @@ -1,6 +1,7 @@ import express, { Router } from "express"; import AuthRouter from "./auth/AuthRouter"; import GuildsRouter from "./guilds/GuildsRouter"; +import GuildRouter from "./guilds/GuildRouter"; export default class MainRouter { public router: Router; @@ -12,6 +13,7 @@ export default class MainRouter { private initializeRoutes() { this.router.use("/auth", AuthRouter); this.router.use("/guilds", GuildsRouter); + this.router.use("/guild", GuildRouter); } } diff --git a/src/api/routes/auth/AuthRouter.ts b/src/api/routes/auth/AuthRouter.ts index 256ea13c6..8ec893a52 100644 --- a/src/api/routes/auth/AuthRouter.ts +++ b/src/api/routes/auth/AuthRouter.ts @@ -1,5 +1,6 @@ import { Router, Request, Response } from "express"; import passport from "passport"; +import config from "../../../config"; class AuthRouter { public router: Router; @@ -13,13 +14,13 @@ class AuthRouter { res.sendStatus(200); }); this.router.get("/redirect", passport.authenticate("discord"), (req, res) => { - res.redirect("/dashboard"); + res.redirect(`${config.dashboard.website}/dashboard`); }); this.router.get("/me", this.getLogin); } private getHello(req: Request, res: Response) { - res.json({ message: "Hello World" }); + res.json({ message: "lavamusic api v1 made by devblacky" }); } private async getLogin(req: Request, res: Response) { diff --git a/src/api/routes/guilds/GuildRouter.ts b/src/api/routes/guilds/GuildRouter.ts new file mode 100644 index 000000000..10250477a --- /dev/null +++ b/src/api/routes/guilds/GuildRouter.ts @@ -0,0 +1,27 @@ +import { Router, Request, Response } from "express"; +import { IsAuth } from "../../middlewares/Middlewares"; +import { getGuild, getGuildMembers } from "../../services/guilds/GuildServices"; + + +class GuildRouter { + public router: Router; + constructor() { + this.router = Router(); + this.initializeRoutes(); + } + private initializeRoutes() { + this.router.get("/:id", IsAuth, this.getGuild); + this.router.get("/:id/members", IsAuth, this.getGuildsMembers); + } + private async getGuild(req: Request, res: Response) { + const guild = await getGuild(req.params.id); + return res.send(guild); + } + private async getGuildsMembers(req: Request, res: Response) { + const guild = await getGuildMembers(req.params.id); + return res.send(guild); + } +} + +const guildRouter = new GuildRouter(); +export default guildRouter.router; \ No newline at end of file diff --git a/src/api/routes/guilds/GuildsRouter.ts b/src/api/routes/guilds/GuildsRouter.ts index e2a8b2ebf..48e5e8038 100644 --- a/src/api/routes/guilds/GuildsRouter.ts +++ b/src/api/routes/guilds/GuildsRouter.ts @@ -12,10 +12,6 @@ class GuildsRouter { private initializeRoutes() { this.router.get("/", IsAuth, getGuildController); } - - private getHello(req: Request, res: Response) { - res.json({ message: "Hello World" }); - } } const guildsRouter = new GuildsRouter(); diff --git a/src/api/services/guilds/GuildServices.ts b/src/api/services/guilds/GuildServices.ts index 8a31da223..f096d2131 100644 --- a/src/api/services/guilds/GuildServices.ts +++ b/src/api/services/guilds/GuildServices.ts @@ -35,6 +35,18 @@ export async function getGuildChannels(guildId: string) { return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/channels`, `Bot ${config.token}`); } +export async function getGuildRoles(guildId: string) { + return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/roles`, `Bot ${config.token}`); +} + +export async function getGuildMembers(guildId: string) { + return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/members`, `Bot ${config.token}`); +} + +export async function getGuildMember(guildId: string, userId: string) { + return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/members/${userId}`, `Bot ${config.token}`); +} + export async function fetchRequest(url: string, auth): Promise { const response = await fetch(url, { method: "GET", diff --git a/src/config.ts b/src/config.ts index de56015aa..a3ff26e66 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,9 +15,10 @@ export default { searchEngine: process.env.SEARCH_ENGINE || (SearchEngine.YouTube as SearchEngine), dashboard: { port: parseInt(process.env.DASHBOARD_PORT) || 3001, - website: process.env.DASHBOARD_WEBSITE || 'http://localhost', + website: process.env.DASHBOARD_WEBSITE || 'http://localhost:3000', + host: process.env.DASHBOARD_HOST || 'http://localhost', enable: parseBoolean(process.env.DASHBOARD_ENABLE) || true, - redirectUri: process.env.DASHBOARD_REDIRECT_URI || 'http://localhost:3000/api/auth/redirect', + redirectUri: process.env.DASHBOARD_REDIRECT_URI || 'http://localhost:3001/api/auth/redirect', sessionSecret: "AMVCXSZXGCHGCTEAEWATRCHVKHGXHDTEWGCJGDZDS", }, maxPlaylistSize: parseInt(process.env.MAX_PLAYLIST_SIZE) || 100,