Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Aug 27, 2023
1 parent 13e0ba4 commit 64a5948
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/api/controllers/guilds/GuildControllers.ts
Original file line number Diff line number Diff line change
@@ -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" });
Expand Down
39 changes: 16 additions & 23 deletions src/api/services/guilds/GuildServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<any> {
let res = await fetch(url, {
headers: {
Authorization: `Bot ${config.token}`

Authorization: auth
}
});
let data = await res.json();
Expand Down

0 comments on commit 64a5948

Please sign in to comment.