From 64a594896e609e9a4490c9089ff91cb2d9525566 Mon Sep 17 00:00:00 2001 From: moe-music Date: Mon, 28 Aug 2023 02:48:28 +0530 Subject: [PATCH] new --- .../controllers/guilds/GuildControllers.ts | 9 +++-- src/api/services/guilds/GuildServices.ts | 39 ++++++++----------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/api/controllers/guilds/GuildControllers.ts b/src/api/controllers/guilds/GuildControllers.ts index 82355b871..e782ae764 100644 --- a/src/api/controllers/guilds/GuildControllers.ts +++ b/src/api/controllers/guilds/GuildControllers.ts @@ -1,13 +1,14 @@ import { Request, Response } from "express"; -import { getBotGuildsService, getUserGuildsService } from "../../services/guilds/GuildServices"; +import { getMutualGuildsService } from "../../services/guilds/GuildServices"; export async function getGuildController(req: Request, res: Response) { try { const user = req.user as any; - const data = await getBotGuildsService(); - const userGuilds = getUserGuildsService(user.id); - res.send({ data, userGuilds }); + const data = await getMutualGuildsService(user.id); + res.send({ + data: data + }); } catch (error) { console.log(error); res.sendStatus(400).send({ error: "Bad Request" }); diff --git a/src/api/services/guilds/GuildServices.ts b/src/api/services/guilds/GuildServices.ts index 5602a6a0e..45bfa52a5 100644 --- a/src/api/services/guilds/GuildServices.ts +++ b/src/api/services/guilds/GuildServices.ts @@ -5,13 +5,7 @@ import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); export async function getBotGuildsService() { - let res = await fetch(`${DISCORD_API_URL}/users/@me/guilds`, { - headers: { - Authorization: `Bot ${config.token}` - } - }); - let data = await res.json(); - return data; + return await fetchRequest(`${DISCORD_API_URL}/users/@me/guilds`, `Bot ${config.token}`); } export async function getUserGuildsService(id: string) { @@ -20,31 +14,30 @@ export async function getUserGuildsService(id: string) { id: id } }); + return await fetchRequest(`${DISCORD_API_URL}/users/@me/guilds`, `Bearer ${user.accessToken}`); +} - let res = await fetch(`${DISCORD_API_URL}/users/@me/guilds`, { - headers: { - Authorization: `Bearer ${user.accessToken}` - } +export async function getMutualGuildsService(id: string) { + const botGuilds = await getBotGuildsService(); + const userGuilds = await getUserGuildsService(id); + const adminUserGuilds = userGuilds.filter(({ permissions }) => (parseInt(permissions) & 0x8) === 0x8); + return adminUserGuilds.filter((guild) => { + return botGuilds.some((botGuild) => botGuild.id === guild.id); }); - let data = await res.json(); - return data; } export async function getGuild(guildId: string) { - let res = await fetch(`${DISCORD_API_URL}/guilds/${guildId}`, { - headers: { - Authorization: `Bot ${config.token}` - } - }); - let data = await res.json(); - return data; + return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}`, `Bot ${config.token}`); } export async function getGuildChannels(guildId: string) { - let res = await fetch(`${DISCORD_API_URL}/guilds/${guildId}/channels`, { + return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/channels`, `Bot ${config.token}`); +} + +export async function fetchRequest(url: string, auth): Promise { + let res = await fetch(url, { headers: { - Authorization: `Bot ${config.token}` - + Authorization: auth } }); let data = await res.json();